:root {
  --navy: #283044;
  --steel: #78A1BB;
  --mint: #EBF5EE;
  --khaki: #BFA89E;
  --taupe: #8B786D;

  --bg: #F4F8F5;
  --surface: #FFFFFF;
  --surface-2: #EBF5EE;
  --text: #283044;
  --muted: #6B7280;
  --line: #DDE5E0;
  --accent: #283044;
  --accent-text: #FFFFFF;

  --veg: #6E9F7B;
  --veg-soft: #E2EFE5;
  --protein: #B5836A;
  --protein-soft: #F3E7E0;
  --starch: #C9A24B;
  --starch-soft: #F6EED9;
  --ok: #4F8A5B;
  --warn: #B7791F;
  --bad: #B4534B;

  --radius: 14px;
  --shadow: 0 1px 2px rgba(40, 48, 68, .06), 0 4px 14px rgba(40, 48, 68, .06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1B2030;
    --surface: #242A3C;
    --surface-2: #2C3348;
    --text: #E8EEF0;
    --muted: #9AA4B2;
    --line: #353D54;
    --accent: #78A1BB;
    --accent-text: #1B2030;
    --veg-soft: #2C3D34;
    --protein-soft: #3F3330;
    --starch-soft: #3E3826;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: 'Noto Sans TC', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--bad); min-height: 1.2em; font-size: 14px; }
h2 { font-size: 18px; margin: 0 0 8px; }
h3 { font-size: 16px; margin: 0 0 6px; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: var(--navy); }
.login-card { background: var(--surface); padding: 32px 24px; border-radius: 20px; width: 100%; max-width: 340px; text-align: center; display: grid; gap: 12px; }
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0; }
.brand-mark { width: 64px; height: 64px; margin: 0 auto; border-radius: 50%; display: grid; place-items: center; font-weight: 700; color: #fff;
  background: conic-gradient(var(--veg) 0 50%, var(--protein) 50% 75%, var(--starch) 75% 100%); }

/* Layout */
.topbar { position: sticky; top: 0; z-index: 5; background: var(--navy); color: #fff; padding: 14px 16px; padding-top: max(14px, env(safe-area-inset-top)); }
.topbar-title { font-weight: 700; font-size: 17px; max-width: 760px; margin: 0 auto; }
.view { max-width: 760px; margin: 0 auto; padding: 16px 16px calc(84px + env(safe-area-inset-bottom)); }
.tabbar { position: fixed; bottom: 0; left: 0; right: 0; z-index: 5; display: grid; grid-template-columns: repeat(5, 1fr); background: var(--surface); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
.tabbar button { background: none; border: 0; padding: 8px 4px 10px; font: inherit; font-size: 12px; color: var(--muted); display: grid; justify-items: center; gap: 2px; cursor: pointer; }
.tabbar .ico { font-size: 18px; line-height: 1; }
.tabbar button.active { color: var(--text); font-weight: 700; }

/* Controls */
.btn { font: inherit; border: 1px solid var(--line); background: var(--surface); color: var(--text); padding: 9px 14px; border-radius: 10px; cursor: pointer; font-weight: 500; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.danger { color: var(--bad); border-color: var(--bad); background: transparent; }
.btn.sm { padding: 5px 10px; font-size: 13px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; }
input, select { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 9px 10px; width: 100%; }
input:focus, select:focus { outline: 2px solid var(--steel); outline-offset: 0; }
label.field { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.row { display: flex; align-items: center; gap: 8px; }
.row.wrap { flex-wrap: wrap; }
.spacer { flex: 1; }
.stack { display: grid; gap: 12px; }

.seg { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { font: inherit; font-size: 14px; border: 0; background: transparent; color: var(--text); padding: 6px 12px; border-radius: 8px; cursor: pointer; }
.seg button.on { background: var(--surface); font-weight: 700; box-shadow: var(--shadow); }

.chip { font: inherit; font-size: 13px; border: 1px solid var(--line); background: var(--surface); color: var(--text); padding: 5px 11px; border-radius: 999px; cursor: pointer; white-space: nowrap; }
.chip.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chips.scroll { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; }

.card { background: var(--surface); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); border: 1px solid var(--line); }
.card + .card { margin-top: 10px; }
.section-title { font-size: 13px; font-weight: 700; color: var(--muted); margin: 18px 0 8px; letter-spacing: .04em; }

.badge { display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 999px; font-weight: 500; white-space: nowrap; }
.badge.ready { background: #DFF0E2; color: var(--ok); }
.badge.near { background: #F8ECD4; color: var(--warn); }
.badge.far, .badge.tool { background: #F5E0DE; color: var(--bad); }
.badge.veg { background: var(--veg-soft); color: var(--veg); }
.badge.protein { background: var(--protein-soft); color: var(--protein); }
.badge.starch { background: var(--starch-soft); color: var(--starch); }
.badge.combo { background: var(--surface-2); color: var(--text); }
@media (prefers-color-scheme: dark) {
  .badge.ready { background: #2A4232; }
  .badge.near { background: #4A3D22; }
  .badge.far, .badge.tool { background: #4A2C2A; }
}

.banner { background: var(--starch-soft); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 12px; font-size: 14px; }
.banner a { color: inherit; font-weight: 700; }

/* Plan */
.plan-controls { display: grid; gap: 10px; margin-bottom: 14px; }
.plate-wrap { display: grid; grid-template-columns: 150px 1fr; gap: 16px; align-items: center; }
.plate { width: 150px; height: 150px; border-radius: 50%; position: relative; border: 6px solid var(--surface-2);
  background: conic-gradient(var(--veg) 0 50%, var(--protein) 50% 75%, var(--starch) 75% 100%); }
.plate span { position: absolute; color: #fff; font-size: 12px; font-weight: 700; text-shadow: 0 1px 2px rgba(0,0,0,.25); }
.plate .pv { right: 14px; top: 58px; }
.plate .pp { left: 22px; bottom: 34px; }
.plate .ps { left: 22px; top: 34px; }
.legend { display: grid; gap: 6px; font-size: 14px; }
.legend .dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; margin-right: 6px; }

.dish { border-left: 4px solid var(--line); }
.dish.veg { border-left-color: var(--veg); }
.dish.protein, .dish.combo { border-left-color: var(--protein); }
.dish.starch { border-left-color: var(--starch); }
.dish-head { display: flex; align-items: flex-start; gap: 8px; }
.dish-name { font-weight: 700; font-size: 16px; }
.dish-meta { font-size: 13px; color: var(--muted); }
.portion-table { width: 100%; border-collapse: collapse; font-size: 14px; margin-top: 8px; }
.portion-table th, .portion-table td { text-align: left; padding: 5px 4px; border-bottom: 1px solid var(--line); }
.portion-table th { font-weight: 500; color: var(--muted); font-size: 12px; }
.portion-table td.num, .portion-table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.portion-table tr.seasoning td { color: var(--muted); font-size: 13px; }
.steps { margin: 8px 0 0; padding-left: 20px; }
.steps li { margin: 3px 0; }
details summary { cursor: pointer; font-size: 14px; color: var(--steel); font-weight: 500; margin-top: 8px; }
.missing { color: var(--bad); font-size: 13px; }
.short { color: var(--warn); font-size: 12px; }

.person-sum { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px; }
.kcal-big { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }
.bar { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; margin: 6px 0; }
.bar > div { height: 100%; background: var(--steel); }
.bar > div.over { background: var(--bad); }
.macro { display: flex; gap: 12px; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }

/* Pantry */
.search { position: relative; margin-bottom: 10px; }
.food-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.food-row:last-child { border-bottom: 0; }
.food-name { font-weight: 500; cursor: pointer; }
.food-sub { font-size: 12px; color: var(--muted); }
.toggle { font: inherit; font-size: 13px; min-width: 56px; border: 1px solid var(--line); border-radius: 999px; padding: 4px 10px; background: var(--surface); color: var(--muted); cursor: pointer; }
.toggle.on { background: var(--ok); border-color: var(--ok); color: #fff; }
.stock-input { width: 84px; padding: 5px 8px; font-size: 14px; text-align: right; }
.season-grid { display: flex; flex-wrap: wrap; gap: 8px; }
.season-grid .chip.on { background: var(--ok); border-color: var(--ok); color: #fff; }

/* Recipes */
.recipe-card { cursor: pointer; }
.recipe-card .row { align-items: flex-start; }

/* Settings */
.target-box { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; font-size: 14px; display: grid; gap: 2px; }

/* Modal */
.modal { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; justify-content: center; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(20, 24, 36, .5); }
.modal-sheet { position: relative; background: var(--bg); width: 100%; max-width: 640px; max-height: 90vh; overflow-y: auto; border-radius: 20px 20px 0 0; padding: 20px 16px calc(24px + env(safe-area-inset-bottom)); }
.modal-close { position: absolute; right: 10px; top: 8px; border: 0; background: none; font-size: 26px; color: var(--muted); cursor: pointer; }
@media (min-width: 700px) {
  .modal { align-items: center; }
  .modal-sheet { border-radius: 20px; }
}

.toast { position: fixed; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: var(--navy); color: #fff; padding: 9px 16px; border-radius: 999px; font-size: 14px; z-index: 30; max-width: calc(100% - 32px); }

/* Calendar */
.cal-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.cal-head .month { font-weight: 700; font-size: 17px; flex: 1; text-align: center; }
.cal { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal .dow { text-align: center; font-size: 12px; color: var(--muted); padding: 2px 0; }
.cal-cell { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; min-height: 58px; padding: 4px 3px; cursor: pointer; display: grid; align-content: start; gap: 1px; font: inherit; color: var(--text); text-align: center; }
.cal-cell.out { opacity: .35; }
.cal-cell.today .dnum { background: var(--steel); color: #fff; border-radius: 999px; padding: 0 6px; }
.cal-cell.sel { outline: 2px solid var(--accent); outline-offset: -1px; }
.cal-cell .dnum { font-size: 13px; font-weight: 700; justify-self: center; }
.cal-cell .k { font-size: 11px; font-variant-numeric: tabular-nums; line-height: 1.3; }
.k.ok { color: var(--ok); }
.k.over { color: var(--bad); font-weight: 700; }
.k.under { color: var(--muted); }
.meal-block { padding: 8px 0; border-bottom: 1px solid var(--line); }
.meal-block:last-child { border-bottom: 0; }
.meal-tag { font-size: 12px; color: var(--muted); }
.log-line { display: flex; gap: 8px; align-items: flex-start; }
.log-items { font-size: 13px; color: var(--muted); }
.icon-btn { border: 0; background: none; color: var(--muted); font-size: 18px; cursor: pointer; padding: 0 4px; line-height: 1; }

/* Log editor */
.ed-line { border: 1px solid var(--line); border-radius: 12px; padding: 10px; background: var(--surface); }
.ed-line + .ed-line { margin-top: 8px; }
.ed-people { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px; margin-top: 6px; }
.ed-people label { font-size: 12px; color: var(--muted); display: grid; gap: 2px; }
.ed-people input { padding: 6px 8px; }
.ed-add { display: grid; gap: 8px; background: var(--surface-2); border-radius: 12px; padding: 10px; }

.empty { text-align: center; color: var(--muted); padding: 24px 8px; font-size: 14px; }

@media (max-width: 420px) {
  .plate-wrap { grid-template-columns: 110px 1fr; gap: 12px; }
  .plate { width: 110px; height: 110px; }
  .plate span { display: none; }
}
