:root {
  --bg: #0f0f0f;
  --surface: #1a1a1a;
  --surface-2: #252525;
  --surface-3: #333333;
  --text: #f5f5f5;
  --text-muted: #999999;
  --accent: #ff6b4a;
  --accent-dark: #e55a3b;
  --success: #4ade80;
  --warning: #fbbf24;
  --danger: #f87171;
  --radius: 14px;
  --radius-sm: 8px;
  --shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  min-height: 100vh;
}

body {
  padding-bottom: env(safe-area-inset-bottom, 0);
}

#app {
  max-width: 600px;
  margin: 0 auto;
  padding: 16px;
}

.app-header {
  text-align: center;
  padding: 24px 0 16px;
}

.app-header h1 {
  margin: 0;
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.app-header .subtitle {
  margin: 4px 0 0;
  color: var(--text-muted);
  font-size: 0.9rem;
}

section {
  margin-bottom: 24px;
}

section h2 {
  font-size: 1.1rem;
  font-weight: 600;
  margin: 0 0 12px;
  color: var(--text);
}

section h3 {
  font-size: 0.95rem;
  font-weight: 600;
  margin: 0 0 10px;
  color: var(--text-muted);
}

/* Stats Card */
.stats-card {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}

.stat {
  text-align: center;
}

.stat-value {
  display: block;
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--accent);
  line-height: 1.2;
}

.stat-label {
  display: block;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 4px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

/* Add Section */
.add-section {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}

.category-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 16px;
}

.category-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 14px 8px;
  background: var(--surface-2);
  border: 2px solid transparent;
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
  min-height: 76px;
}

.category-btn:active {
  transform: scale(0.98);
}

.category-btn.selected {
  border-color: var(--accent);
  background: rgba(255, 107, 74, 0.12);
}

.category-icon {
  font-size: 1.5rem;
  line-height: 1;
}

.category-name {
  text-align: center;
  line-height: 1.2;
}

.selected-category {
  text-align: center;
  padding: 10px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-bottom: 12px;
}

.selected-category.has-selection {
  color: var(--accent);
  font-weight: 600;
}

.add-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.form-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-row label {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.form-row input,
.form-row select {
  width: 100%;
  padding: 12px 14px;
  background: var(--surface-2);
  border: 1px solid var(--surface-3);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s ease;
}

.form-row input:focus,
.form-row select:focus {
  border-color: var(--accent);
}

input[type="datetime-local"] {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  box-sizing: border-box;
  -webkit-appearance: none;
  appearance: none;
}

.count-control {
  display: flex;
  align-items: center;
  gap: 8px;
}

.count-control input {
  flex: 1;
  text-align: center;
  font-size: 1.25rem;
  font-weight: 600;
  padding: 10px;
}

.count-btn {
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface-3);
  border: none;
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 1.25rem;
  font-weight: 600;
  cursor: pointer;
}

.count-btn:active {
  background: var(--accent);
}

.add-btn {
  width: 100%;
  padding: 14px;
  background: var(--accent);
  border: none;
  border-radius: var(--radius-sm);
  color: white;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease;
  margin-top: 4px;
}

.add-btn:disabled {
  background: var(--surface-3);
  color: var(--text-muted);
  cursor: not-allowed;
}

.add-btn:not(:disabled):active {
  background: var(--accent-dark);
}

/* Log Section */
.log-section,
.history-section,
.insights-section,
.data-section {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.section-header h2,
.section-header h3 {
  margin: 0;
}

.badge {
  background: var(--surface-2);
  color: var(--text-muted);
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 500;
}

.log-list,
.days-list,
.trigger-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.log-entry {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  gap: 12px;
}

.log-entry-info {
  flex: 1;
  min-width: 0;
}

.log-entry-category {
  font-weight: 600;
  font-size: 0.95rem;
  color: var(--text);
}

.log-entry-note {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.log-entry-time {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 4px;
}

.log-entry-count {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--accent);
  min-width: 32px;
  text-align: right;
}

.log-entry-actions {
  display: flex;
  gap: 6px;
}

.entry-action {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 0.85rem;
  cursor: pointer;
  padding: 4px;
}

.entry-action.delete {
  color: var(--danger);
}

.empty-state {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.9rem;
  padding: 16px;
  margin: 0;
}

/* History */
.export-controls {
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 12px;
  margin-bottom: 16px;
}

.custom-range {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 12px;
}

.custom-range.hidden {
  display: none;
}

.export-buttons {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 12px;
}

.export-btn {
  padding: 10px;
  background: var(--surface-3);
  border: none;
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
}

.export-btn.json {
  color: #7dd3fc;
}

.export-btn.csv {
  color: #86efac;
}

.export-btn.md {
  color: #f9a8d4;
}

.export-btn:active {
  background: var(--accent);
  color: white;
}

.day-card {
  padding: 12px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
}

.day-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}

.day-card-date {
  font-weight: 600;
  font-size: 0.95rem;
}

.day-card-count {
  font-weight: 700;
  color: var(--accent);
}

.day-card-cats {
  font-size: 0.8rem;
  color: var(--text-muted);
}

/* Insights */
.insights-card {
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 14px;
  margin-bottom: 12px;
}

.insights-card:last-child {
  margin-bottom: 0;
}

.trigger-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid var(--surface-3);
}

.trigger-item:last-child {
  border-bottom: none;
}

.trigger-name {
  display: flex;
  align-items: center;
  gap: 8px;
}

.trigger-bar {
  height: 6px;
  background: var(--surface-3);
  border-radius: 999px;
  overflow: hidden;
  width: 80px;
  margin-left: 8px;
}

.trigger-bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 999px;
  transition: width 0.3s ease;
}

.trigger-count {
  font-weight: 600;
  color: var(--accent);
}

.time-of-day {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text);
}

.daily-avg {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.daily-avg .stat-value {
  font-size: 2rem;
}

.daily-avg .stat-label {
  display: inline;
  font-size: 0.9rem;
  text-transform: none;
}

/* Data Section */
.help-text {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin: 0 0 12px;
  line-height: 1.5;
}

.data-buttons {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.data-btn {
  padding: 12px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
}

.data-btn.secondary {
  background: var(--surface-2);
  color: var(--text);
}

.data-btn.danger {
  background: rgba(248, 113, 113, 0.15);
  color: var(--danger);
}

.data-btn:active {
  opacity: 0.8;
}

/* Footer */
.app-footer {
  text-align: center;
  padding: 16px 0 32px;
  color: var(--text-muted);
  font-size: 0.8rem;
}

/* Confirm Dialog */
.confirm-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 1000;
}

.confirm-dialog {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px;
  max-width: 320px;
  width: 100%;
  box-shadow: var(--shadow);
}

.confirm-dialog h3 {
  margin: 0 0 10px;
  font-size: 1.1rem;
}

.confirm-dialog p {
  margin: 0 0 16px;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.confirm-buttons {
  display: flex;
  gap: 10px;
}

.confirm-buttons button {
  flex: 1;
  padding: 10px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
}

.confirm-buttons .confirm-cancel {
  background: var(--surface-2);
  color: var(--text);
}

.confirm-buttons .confirm-ok {
  background: var(--danger);
  color: white;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--surface-2);
  color: var(--text);
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  font-size: 0.9rem;
  z-index: 1001;
  opacity: 0;
  transition: all 0.3s ease;
  pointer-events: none;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

/* Responsive */
@media (min-width: 480px) {
  .category-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}
