:root {
  --bg: #0f172a;
  --bg2: #1e293b;
  --bg3: #334155;
  --accent: #6366f1;
  --accent2: #818cf8;
  --green: #22c55e;
  --red: #ef4444;
  --orange: #f97316;
  --text: #f1f5f9;
  --text2: #94a3b8;
  --border: #334155;
  --card: #1e293b;
  --radius: 12px;
  --nav-h: 64px;
}

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

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.5;
}

/* Layout */
#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 480px;
  margin: 0 auto;
  position: relative;
}

#views {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding-bottom: calc(var(--nav-h) + 8px);
  -webkit-overflow-scrolling: touch;
}

/* Navigation */
#nav {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 480px;
  height: var(--nav-h);
  background: var(--bg2);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  z-index: 100;
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.nav-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 8px 4px;
  background: none;
  border: none;
  color: var(--text2);
  font-size: 11px;
  cursor: pointer;
  transition: color 0.15s;
}

.nav-btn .icon {
  font-size: 22px;
  line-height: 1;
}

.nav-btn.active {
  color: var(--accent2);
}

/* Views */
.view {
  display: none;
  padding: 16px;
  min-height: 100%;
}

.view.active {
  display: block;
}

/* Header */
.view-header {
  margin-bottom: 20px;
}

.view-header h1 {
  font-size: 22px;
  font-weight: 700;
}

.view-header .subtitle {
  color: var(--text2);
  font-size: 13px;
  margin-top: 2px;
}

/* Cards */
.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  border: 1px solid var(--border);
}

.card-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 12px;
}

/* Input row */
.input-row {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}

.input-row input {
  flex: 1;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  color: var(--text);
  font-size: 15px;
  outline: none;
  transition: border-color 0.15s;
}

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

.input-row input::placeholder {
  color: var(--text2);
}

/* Items list */
.items-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.list-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: var(--bg3);
  border-radius: 8px;
  font-size: 14px;
}

.list-item .item-text {
  flex: 1;
}

.list-item .del-btn {
  background: none;
  border: none;
  color: var(--text2);
  cursor: pointer;
  font-size: 16px;
  padding: 2px 4px;
  line-height: 1;
  opacity: 0.6;
  transition: opacity 0.15s;
}

.list-item .del-btn:hover {
  opacity: 1;
  color: var(--red);
}

.empty-hint {
  color: var(--text2);
  font-size: 13px;
  text-align: center;
  padding: 12px 0 4px;
}

/* Buttons */
.btn {
  width: 100%;
  padding: 13px 16px;
  border-radius: var(--radius);
  border: none;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

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

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}

.btn-secondary {
  background: var(--bg3);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-danger {
  background: transparent;
  color: var(--red);
  border: 1px solid var(--red);
}

.btn-sm {
  width: auto;
  padding: 8px 14px;
  font-size: 14px;
  border-radius: 8px;
}

/* Weight row */
.weight-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.weight-row label {
  color: var(--text2);
  font-size: 14px;
  white-space: nowrap;
}

.weight-row input {
  width: 100px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  color: var(--text);
  font-size: 15px;
  outline: none;
  transition: border-color 0.15s;
}

.weight-row input:focus {
  border-color: var(--accent);
}

/* AI result */
#ai-result {
  display: none;
}

#ai-result.visible {
  display: block;
}

.ai-content {
  color: var(--text);
  font-size: 14px;
  line-height: 1.7;
  white-space: pre-wrap;
}

.ai-content strong {
  color: var(--accent2);
}

/* Stats row */
.stats-row {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.stat-box {
  flex: 1;
  background: var(--bg3);
  border-radius: 8px;
  padding: 10px 8px;
  text-align: center;
}

.stat-box .val {
  font-size: 20px;
  font-weight: 700;
  color: var(--accent2);
}

.stat-box .lbl {
  font-size: 11px;
  color: var(--text2);
  margin-top: 2px;
}

/* Calorie bar */
.cal-bar-wrap {
  margin: 10px 0;
}

.cal-bar-labels {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text2);
  margin-bottom: 5px;
}

.cal-bar {
  height: 8px;
  background: var(--bg3);
  border-radius: 4px;
  overflow: hidden;
}

.cal-bar-fill {
  height: 100%;
  border-radius: 4px;
  transition: width 0.4s ease;
  background: var(--green);
}

.cal-bar-fill.over {
  background: var(--red);
}

.cal-bar-fill.close {
  background: var(--orange);
}

/* Loading spinner */
.spinner {
  display: inline-block;
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255,255,255,0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* History */
.history-day {
  background: var(--card);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  margin-bottom: 10px;
  overflow: hidden;
}

.history-day-header {
  display: flex;
  align-items: center;
  padding: 14px 16px;
  cursor: pointer;
  gap: 12px;
  user-select: none;
}

.history-day-header:active {
  background: var(--bg3);
}

.history-date {
  font-weight: 600;
  font-size: 15px;
  flex: 1;
}

.history-meta {
  font-size: 13px;
  color: var(--text2);
  text-align: right;
  line-height: 1.4;
}

.history-meta .kcal {
  color: var(--accent2);
  font-weight: 600;
}

.history-chevron {
  color: var(--text2);
  font-size: 12px;
  transition: transform 0.2s;
}

.history-day.expanded .history-chevron {
  transform: rotate(180deg);
}

.history-detail {
  display: none;
  padding: 0 16px 14px;
  border-top: 1px solid var(--border);
}

.history-day.expanded .history-detail {
  display: block;
}

.history-detail-section {
  margin-top: 10px;
}

.history-detail-section .lbl {
  font-size: 12px;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 4px;
}

.history-detail-section .val {
  font-size: 13px;
  line-height: 1.6;
}

.history-ai {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  font-size: 13px;
  color: var(--text2);
  line-height: 1.6;
  white-space: pre-wrap;
}

/* Settings */
.settings-group {
  margin-bottom: 20px;
}

.settings-group h2 {
  font-size: 12px;
  font-weight: 600;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 10px;
}

.settings-field {
  margin-bottom: 10px;
}

.settings-field label {
  display: block;
  font-size: 14px;
  margin-bottom: 5px;
  color: var(--text2);
}

.settings-field input {
  width: 100%;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 11px 14px;
  color: var(--text);
  font-size: 15px;
  outline: none;
  transition: border-color 0.15s;
}

.settings-field input:focus {
  border-color: var(--accent);
}

.settings-field input::placeholder {
  color: var(--text2);
}

.save-notice {
  font-size: 12px;
  color: var(--green);
  text-align: center;
  margin-top: 6px;
  min-height: 18px;
  transition: opacity 0.3s;
}

/* Toast */
#toast {
  position: fixed;
  bottom: calc(var(--nav-h) + 16px);
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 20px;
  font-size: 14px;
  z-index: 200;
  opacity: 0;
  transition: opacity 0.25s, transform 0.25s;
  pointer-events: none;
  white-space: nowrap;
  max-width: 90vw;
}

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

/* Divider */
.divider {
  height: 1px;
  background: var(--border);
  margin: 12px 0;
}

/* Settings cards */
.settings-card {
  background: var(--card);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  overflow: hidden;
  margin: 0 16px;
}

.settings-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  cursor: pointer;
  font-weight: 600;
  font-size: 15px;
}

.settings-chevron {
  color: var(--text2);
  font-size: 12px;
  transition: transform 0.2s;
}

.settings-card.open .settings-chevron {
  transform: rotate(180deg);
}

.settings-card-body {
  display: none;
  padding: 0 16px 16px;
}

.settings-card.open .settings-card-body {
  display: block;
}

/* Analyze button in food list */
.analyze-btn {
  background: none;
  border: none;
  font-size: 14px;
  cursor: pointer;
  padding: 2px 4px;
  opacity: 0.5;
  transition: opacity 0.15s;
}

.analyze-btn:active {
  opacity: 1;
}

/* Day nav */
.btn-icon {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  cursor: pointer;
}

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

/* Chips */
.chips-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
  position: relative;
}

.chips-row:empty {
  display: none;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 8px 12px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}

.chip:active {
  transform: scale(0.95);
}

.chip.today {
  border-color: var(--accent);
  background: rgba(99, 102, 241, 0.15);
  color: var(--accent2);
}

/* Picker dropdown */
.picker-dropdown {
  display: none;
  position: absolute;
  left: 0;
  right: 0;
  z-index: 50;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-top: 4px;
  max-height: 200px;
  overflow-y: auto;
  box-shadow: 0 8px 24px rgba(0,0,0,0.3);
}

.picker-dropdown.visible {
  display: block;
}

.picker-item {
  padding: 10px 14px;
  font-size: 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}

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

.picker-item:active {
  background: var(--bg3);
}

/* Suggest dropdown */
.suggest-dropdown {
  display: none;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-top: 4px;
  max-height: 160px;
  overflow-y: auto;
}

.suggest-dropdown.visible {
  display: block;
}

.suggest-item {
  padding: 10px 14px;
  font-size: 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}

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

.suggest-item:active {
  background: var(--bg3);
}

/* History tabs */
.history-tabs {
  display: flex;
  gap: 0;
  margin: 0 16px 12px;
  background: var(--bg3);
  border-radius: 10px;
  padding: 3px;
}

.history-tab {
  flex: 1;
  padding: 8px 0;
  border: none;
  background: transparent;
  color: var(--text2);
  font-size: 14px;
  font-weight: 600;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s;
}

.history-tab.active {
  background: var(--accent);
  color: #fff;
}

.history-tab-content {
  display: none;
}

.history-tab-content.active {
  display: block;
}

/* Gap utils */
.mb8 { margin-bottom: 8px; }
.mb12 { margin-bottom: 12px; }
.mb16 { margin-bottom: 16px; }
