*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --border: #334155;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --primary: #38bdf8;
  --primary-hover: #0ea5e9;
  --primary-bg: rgba(56,189,248,0.1);
  --danger: #f87171;
  --danger-hover: #ef4444;
  --success: #4ade80;
  --warning: #fbbf24;
  --radius: 10px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px 16px;
}

header {
  display: flex;
  align-items: center;
  gap: 24px;
  flex-wrap: wrap;
  margin-bottom: 24px;
}

header h1 {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--primary);
  white-space: nowrap;
}

nav {
  display: flex;
  gap: 4px;
  flex: 1;
}

.tab-spacer { flex: 1; }

#auth-area {
  display: flex;
  align-items: center;
  gap: 8px;
}

.auth-btn {
  padding: 6px 14px;
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}

.auth-btn:hover { background: var(--surface); color: var(--text); }

#auth-user {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.tab {
  padding: 8px 16px;
  background: transparent;
  color: var(--text-muted);
  border: none;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.tab:hover { background: var(--surface); color: var(--text); }
.tab.active { background: var(--primary); color: #0f172a; font-weight: 600; }

.trend-stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin-bottom: 16px;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  text-align: center;
}

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

.stat-value {
  font-size: 1.25rem;
  font-weight: 700;
}

.stat-up { color: var(--danger); }
.stat-down { color: var(--success); }

.chart-area {
  margin-bottom: 16px;
}

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

.window-selector {
  display: flex;
  gap: 4px;
  background: var(--bg);
  border-radius: 8px;
  padding: 3px;
}

.chart-mode {
  display: flex;
  gap: 4px;
  background: var(--bg);
  border-radius: 8px;
  padding: 3px;
}

.win-btn {
  padding: 6px 14px;
  background: transparent;
  color: var(--text-muted);
  border: none;
  border-radius: 6px;
  font-size: 0.8rem;
  cursor: pointer;
}

.win-btn:hover { background: var(--border); color: var(--text); }
.win-btn.active { background: var(--primary); color: #0f172a; }

.chart-wrap {
  position: relative;
  height: 380px;
}

canvas#weight-chart { width: 100% !important; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.card h2 {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 14px;
}

.bottom-row {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 16px;
}

@media (max-width: 768px) {
  .trend-stats { grid-template-columns: repeat(2, 1fr); }
  .bottom-row { grid-template-columns: 1fr; }
  .tab { padding: 8px 12px; font-size: 0.8rem; }
}

.form-group { margin-bottom: 12px; }

.form-group label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 4px;
}

input[type="number"],
input[type="text"],
input[type="datetime-local"],
input[type="password"] {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  transition: border-color 0.15s;
}

input:focus { outline: none; border-color: var(--primary); }

button {
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  background: var(--primary);
  color: #0f172a;
  transition: background 0.15s;
  min-height: 44px;
  min-width: 44px;
}

button:hover { background: var(--primary-hover); }

button.secondary { background: var(--border); color: var(--text); }
button.secondary:hover { background: #475569; }

.btn-row { display: flex; gap: 8px; margin-top: 8px; }
.btn-row button { flex: 1; }

.table-wrap { overflow-x: auto; max-height: 400px; overflow-y: auto; }

table { width: 100%; border-collapse: collapse; }

th, td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  font-size: 0.85rem;
}

th {
  color: var(--text-muted);
  font-weight: 600;
  font-size: 0.75rem;
  text-transform: uppercase;
  position: sticky;
  top: 0;
  background: var(--surface);
}

td .delta { font-size: 0.8rem; }
.delta-up { color: var(--danger); }
.delta-down { color: var(--success); }
.delta-none { color: var(--text-muted); }

.actions-cell { display: flex; gap: 4px; justify-content: flex-end; }

.edit-btn {
  background: transparent; color: var(--primary);
  padding: 4px 8px; font-size: 0.75rem;
  min-height: 36px; min-width: 36px;
}
.edit-btn:hover { background: var(--primary-bg); }

.del-btn {
  background: transparent; color: var(--danger);
  padding: 4px 8px; font-size: 0.75rem;
  min-height: 36px; min-width: 36px;
}
.del-btn:hover { background: rgba(248,113,113,0.15); }

.settings-card {
  max-width: 480px;
  margin-bottom: 16px;
}

.settings-hint {
  margin-top: 12px;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.placeholder {
  text-align: center;
  color: var(--text-muted);
  padding: 60px 20px;
  font-size: 1rem;
}

.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 24px;
}

.bmi-underweight { color: #60a5fa; font-weight: 600; }
.bmi-normal { color: #4ade80; font-weight: 600; }
.bmi-overweight { color: #fbbf24; font-weight: 600; }
.bmi-obese { color: #f87171; font-weight: 600; }
.bmi-label { font-size: 0.75rem; color: var(--text-muted); }

select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
}

select:focus { outline: none; border-color: var(--primary); }

.cal-budget {
  text-align: center;
}

.cal-budget-header {
  margin-bottom: 8px;
}

.cal-budget-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.cal-budget-formula {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 8px;
  margin-bottom: 14px;
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--text-muted);
}

#cal-budget-num { color: var(--text); }
#cal-consumed { color: var(--text); }

#cal-remaining {
  font-size: 2rem;
  margin-left: 4px;
}

.cal-ok { color: #4ade80; }
.cal-warn { color: #fbbf24; }
.cal-over { color: #f87171; }

.cal-progress-bar-wrap {
  height: 14px;
  background: var(--bg);
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 12px;
}

.cal-progress-bar {
  height: 100%;
  border-radius: 8px;
  transition: width 0.5s ease, background 0.3s;
  min-width: 0;
}

.cal-budget-explanation {
  margin-bottom: 16px;
}

.cal-budget-explanation p {
  color: var(--text-muted);
  font-size: 0.85rem;
  line-height: 1.6;
  margin-bottom: 12px;
}

.budget-detail-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px 16px;
  font-size: 0.85rem;
}

.budget-detail-grid .label {
  color: var(--text-muted);
}

.budget-detail-grid .value {
  color: var(--text);
  font-weight: 500;
  text-align: right;
}

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

@media (max-width: 640px) {
  .cal-meals { grid-template-columns: repeat(2, 1fr); }
}

.meal-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  text-align: center;
  transition: border-color 0.15s;
}

.meal-card.expanded { border-color: var(--primary); }

.meal-name {
  font-size: 0.8rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-bottom: 6px;
}

.meal-total {
  font-size: 1.3rem;
  font-weight: 700;
  margin-bottom: 8px;
}

.meal-add-btn {
  padding: 4px 12px;
  font-size: 0.8rem;
}

.meal-form {
  display: none;
  margin-top: 10px;
  text-align: left;
}

.meal-card.expanded .meal-form { display: block; }

.meal-form input {
  margin-bottom: 8px;
}

.meal-form .btn-row { margin-top: 4px; }
.meal-form .btn-row button { padding: 6px 12px; font-size: 0.8rem; }

.cal-table-card .table-wrap { max-height: 300px; }

.login-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  z-index: 1000;
  align-items: center;
  justify-content: center;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  width: 320px;
  max-width: 90vw;
}

.login-card h3 {
  text-align: center;
  margin-bottom: 16px;
  color: var(--text);
}

.glucose-low { color: #f87171; font-weight: 600; }
.glucose-normal { color: #4ade80; font-weight: 600; }
.glucose-high { color: #f87171; font-weight: 600; }

.ai-input-card {
  max-width: 600px;
  margin-bottom: 16px;
}

.ai-input-card textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
  font-family: inherit;
  resize: vertical;
  transition: border-color 0.15s;
}

.ai-input-card textarea:focus { outline: none; border-color: var(--primary); }

.ai-photo-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.ai-photo-btn {
  display: inline-block;
  padding: 8px 16px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  color: var(--text-muted);
  font-size: 0.85rem;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}

.ai-photo-btn:hover { border-color: var(--primary); color: var(--primary); }

.ai-photos {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.ai-photo-slot {
  position: relative;
  width: 80px;
  height: 80px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.ai-photo-slot img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.ai-photo-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: none;
  background: rgba(0,0,0,0.6);
  color: #fff;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  cursor: pointer;
  padding: 0;
}

.ai-photo-slot-name {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0,0,0,0.5);
  color: #fff;
  font-size: 0.65rem;
  padding: 2px 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ai-spinner {
  margin-left: 12px;
  color: var(--text-muted);
  font-size: 0.85rem;
}

.ai-result {
  max-width: 600px;
  width: 100%;
  overflow: hidden;
}

.ai-summary {
  margin-bottom: 12px;
  text-align: center;
}

.ai-total {
  font-size: 1.2rem;
  color: var(--text);
}

.ai-note {
  margin-top: 6px;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.ai-foods {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.ai-food-card {
  display: grid;
  grid-template-columns: 1fr auto auto auto;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  margin-bottom: 0;
}

.ai-food-name {
  font-weight: 600;
  font-size: 0.9rem;
}

.ai-food-portion {
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
}

.ai-food-kcal {
  font-weight: 700;
  font-size: 0.95rem;
  white-space: nowrap;
}

.ai-add-btn {
  padding: 6px 14px;
  font-size: 0.8rem;
  white-space: nowrap;
}

.ai-meal-badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 12px;
  font-size: 0.75rem;
  font-weight: 600;
  white-space: nowrap;
}

.ai-save-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 16px;
}

.save-progress {
  font-size: 0.85rem;
  color: var(--text-muted);
  min-width: 0;
  overflow-wrap: anywhere;
}

.ai-cost {
  margin-bottom: 12px;
  overflow-wrap: anywhere;
}

.ai-thinking {
  margin-bottom: 12px;
  max-width: 100%;
  overflow: hidden;
}

.ai-thinking-label {
  margin-bottom: 8px;
  color: var(--text);
}

.ai-thinking pre {
  max-width: 100%;
  max-height: 180px;
  overflow: auto;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.85rem;
  line-height: 1.55;
  color: var(--text);
}

.ai-edit-table-wrap {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  margin-bottom: 12px;
}

.ai-edit-table {
  min-width: 520px;
}

.ai-edit-table input,
.ai-edit-table select {
  min-width: 0;
}

.ai-edit-name { min-width: 120px; }
.ai-edit-portion { min-width: 90px; }
.ai-edit-kcal { min-width: 64px; }
.ai-edit-meal { min-width: 112px; }

.ai-col-chk {
  width: 44px;
}

.ai-del-row-btn {
  width: 44px;
  padding: 0;
}

.ai-table-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.ai-table-actions button {
  flex: 1 1 160px;
}

.ai-table-actions .save-progress {
  flex: 1 1 100%;
}

.food-layout {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}
@media (max-width: 640px) {
  .food-layout { grid-template-columns: 1fr; }
}

.food-card { margin-bottom: 0; }

.food-catalog-table { width: 100%; border-collapse: collapse; }
.food-catalog-table th { text-align: left; color: var(--text-muted); font-size: 0.75rem; font-weight: 500; padding: 4px 0; }
.food-catalog-table td { padding: 4px 0; border-bottom: 1px solid var(--border); }
.food-catalog-table .food-actions { text-align: right; white-space: nowrap; }

.food-calc-result {
  background: var(--accent-bg);
  border-radius: 6px;
  padding: 6px 10px;
  margin-bottom: 8px;
  text-align: center;
  color: var(--accent);
  font-size: 1rem;
  font-weight: 700;
}
