/* Macro Monster — single-stylesheet design.
   Dark theme tuned for long sessions and mobile screens.
*/
:root {
  --bg: #0b1220;
  --bg-2: #0f172a;
  --panel: #111c33;
  --panel-2: #18233f;
  --border: #1f2b48;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-2: #0ea5e9;
  --ok: #22c55e;
  --warn: #f59e0b;
  --err: #ef4444;
  --cal: #38bdf8;
  --pro: #a78bfa;
  --car: #fbbf24;
  --fat: #f472b6;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 8px 24px rgba(0,0,0,.35);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background:
    radial-gradient(1200px 600px at 80% -10%, rgba(56,189,248,.08), transparent 60%),
    radial-gradient(900px 500px at -10% 30%, rgba(167,139,250,.06), transparent 60%),
    var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); }
code { background: var(--panel-2); padding: 1px 6px; border-radius: 6px; font-size: .9em; }

/* ---------- Auth screen ---------- */
.auth-screen {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
.auth-card {
  width: 100%; max-width: 420px;
  background: linear-gradient(180deg, var(--panel), var(--panel-2));
  border: 1px solid var(--border); border-radius: 16px;
  padding: 28px; box-shadow: var(--shadow);
}
.auth-brand {
  display: flex; align-items: center; gap: 12px;
  margin-bottom: 20px;
}
.auth-brand h1 { margin: 0; font-size: 22px; }
.auth-brand .logo {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), var(--pro));
  color: #0b1220;
}
.auth-tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.auth-tab {
  background: transparent; color: var(--muted); border: none;
  padding: 8px 12px; border-bottom: 2px solid transparent;
  cursor: pointer; font-size: 14px;
}
.auth-tab.is-active { color: var(--text); border-bottom-color: var(--accent); }
.auth-form .form-row { margin-bottom: 12px; }
.auth-error {
  background: rgba(239,68,68,.1); color: #fecaca;
  border: 1px solid rgba(239,68,68,.3);
  padding: 8px 10px; border-radius: 8px; font-size: 13px; margin: 6px 0 12px;
}
.auth-foot { margin-top: 16px; text-align: center; }
.btn.block { display: block; width: 100%; margin-top: 4px; }

/* ---------- User chip in header ---------- */
.user-chip {
  display: inline-flex; align-items: center; gap: 8px;
}
.user-chip .user-name {
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 999px; padding: 4px 10px; font-size: 13px; color: var(--muted);
}
.btn.small { padding: 4px 10px; font-size: 12px; min-height: 0; }

/* ---------- Header / tabs ---------- */
.app-header {
  position: sticky; top: 0; z-index: 10;
  background: rgba(11,18,32,.85);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--border);
  padding: 14px 20px 0;
}
.app-header .header-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand h1 { font-size: 18px; margin: 0; letter-spacing: .2px; }
.brand .logo {
  display: inline-flex; align-items: center; justify-content: center;
  width: 30px; height: 30px; border-radius: 8px;
  background: linear-gradient(135deg, var(--accent), var(--pro));
  color: #0b1220; font-weight: 800;
}

.tabs { display: flex; gap: 4px; margin-top: 12px; overflow-x: auto; }
.tab {
  background: transparent; color: var(--muted);
  border: none; padding: 10px 14px; cursor: pointer;
  border-bottom: 2px solid transparent;
  font-size: 14px; font-weight: 500;
  white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab.is-active { color: var(--text); border-bottom-color: var(--accent); }

/* ---------- Layout ---------- */
.app-main { padding: 20px; max-width: 1100px; margin: 0 auto; }
.view { display: none; }
.view.is-active { display: block; }

.view-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; margin-bottom: 16px; flex-wrap: wrap;
}
.view-head h2 { margin: 0 0 2px; font-size: 22px; }
.view-head-actions { display: flex; gap: 6px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.mt-8 { margin-top: 8px; } .mt-12 { margin-top: 12px; }
.section-title { margin: 22px 0 10px; font-size: 15px; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; }

.row { display: flex; align-items: center; }
.gap { gap: 8px; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 520px) { .two-col { grid-template-columns: 1fr; } }

/* ---------- Buttons / inputs ---------- */
.btn {
  appearance: none; -webkit-appearance: none;
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 8px 14px; font-size: 14px; cursor: pointer;
  transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { background: #1e2a47; }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent-2); border-color: var(--accent-2); color: #001423; font-weight: 600; }
.btn.primary:hover { background: var(--accent); }
.btn.danger { color: #fecaca; border-color: rgba(239,68,68,.4); }
.btn.danger:hover { background: rgba(239,68,68,.12); }

.icon-btn {
  background: transparent; border: 1px solid transparent;
  color: var(--muted); width: 32px; height: 32px; border-radius: 8px;
  font-size: 18px; cursor: pointer; line-height: 1;
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
.icon-btn.danger:hover { background: rgba(239,68,68,.12); color: #fecaca; }

.input, input[type="text"], input[type="number"], input[type="search"], select {
  background: var(--panel); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 9px 12px; font-size: 14px; width: 100%;
  outline: none;
}
.input:focus, input:focus, select:focus { border-color: var(--accent); }
.input.qty { width: 110px; }
.qty-row { display: flex; align-items: center; gap: 8px; }
.form-row { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.form-row label { font-size: 13px; color: var(--muted); }

/* ---------- Panels ---------- */
.panel {
  background: linear-gradient(180deg, var(--panel), var(--panel-2));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px; margin-bottom: 18px;
  box-shadow: var(--shadow);
}
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 12px;
}
.panel h3 { margin: 0 0 10px; font-size: 16px; }
.panel h3:only-child, .panel-head h3 { margin: 0; }

/* ---------- Today: goals grid ---------- */
.goals-grid {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 16px;
}
@media (max-width: 720px) { .goals-grid { grid-template-columns: repeat(2, 1fr); } }
.goal-card {
  background: linear-gradient(180deg, var(--panel), var(--panel-2));
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; display: flex; flex-direction: column; gap: 8px;
  position: relative; overflow: hidden;
}
.goal-card::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px;
  background: var(--cal);
}
.goal-card.cal::before { background: var(--cal); }
.goal-card.pro::before { background: var(--pro); }
.goal-card.car::before { background: var(--car); }
.goal-card.fat::before { background: var(--fat); }

.goal-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; }
.goal-value { display: flex; align-items: baseline; gap: 4px; }
.goal-num { font-size: 26px; font-weight: 700; }
.goal-of { font-size: 13px; }
.progress {
  height: 6px; background: var(--bg-2); border-radius: 999px; overflow: hidden;
}
.progress-bar {
  height: 100%; background: var(--ok); border-radius: 999px;
  transition: width .25s ease;
}
.progress-bar.over { background: var(--err); }
.goal-card.cal .progress-bar { background: var(--cal); }
.goal-card.pro .progress-bar { background: var(--pro); }
.goal-card.car .progress-bar { background: var(--car); }
.goal-card.fat .progress-bar { background: var(--fat); }
.goal-card.cal .progress-bar.over,
.goal-card.pro .progress-bar.over,
.goal-card.car .progress-bar.over,
.goal-card.fat .progress-bar.over { background: var(--err); }

/* ---------- Chips ---------- */
.chip {
  display: inline-flex; align-items: center;
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 999px; padding: 3px 10px; font-size: 12px;
  margin-right: 4px; white-space: nowrap;
}
.chip.cal { color: var(--cal); border-color: rgba(56,189,248,.3); }
.chip.pro { color: var(--pro); border-color: rgba(167,139,250,.3); }
.chip.car { color: var(--car); border-color: rgba(251,191,36,.3); }
.chip.fat { color: var(--fat); border-color: rgba(244,114,182,.3); }

/* ---------- Day log ---------- */
.log-list { display: flex; flex-direction: column; gap: 8px; }
.log-item {
  display: flex; align-items: center; gap: 12px;
  background: var(--bg-2); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 10px 12px;
}
.log-item-main { flex: 1; min-width: 0; }
.log-item-title { font-weight: 600; }
.log-item-detail { margin-top: 2px; }
.log-item-macros { display: flex; flex-wrap: wrap; gap: 4px; }
@media (max-width: 600px) {
  .log-item { flex-wrap: wrap; }
  .log-item-macros { width: 100%; }
}

/* ---------- Cards (meals) ---------- */
.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.card {
  background: linear-gradient(180deg, var(--panel), var(--panel-2));
  border: 1px solid var(--border); border-radius: var(--radius); padding: 16px;
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 10px; gap: 8px; }
.card-title { margin: 0; font-size: 16px; }
.card-macros { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.macro-block { background: var(--bg-2); border-radius: var(--radius-sm); padding: 8px 10px; }
.macro-block-label { margin-bottom: 4px; }
.macro-block-row { display: flex; flex-wrap: wrap; gap: 4px; }
.card-actions { display: flex; gap: 6px; flex-wrap: wrap; }

/* ---------- Ingredients ---------- */
.search-row { display: flex; gap: 8px; margin-bottom: 12px; }
.search-row input { flex: 1; }
.search-results { display: flex; flex-direction: column; gap: 8px; margin-bottom: 16px; }
.usda-row, .ing-row {
  display: flex; align-items: center; gap: 12px;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 10px 12px;
}
.ing-main { flex: 1; min-width: 0; }
.ing-name { font-weight: 600; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.ing-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.ingredient-list { display: flex; flex-direction: column; gap: 8px; }

.tag {
  display: inline-block; background: rgba(56,189,248,.15);
  color: var(--accent); font-size: 11px; padding: 1px 8px;
  border-radius: 999px; text-transform: uppercase; letter-spacing: .05em;
}
.tag.custom { background: rgba(167,139,250,.15); color: var(--pro); }
.tag.brand { background: rgba(251,191,36,.15); color: var(--car); text-transform: none; letter-spacing: 0; }

/* ---------- History ---------- */
.history-list { display: flex; flex-direction: column; gap: 6px; }
.history-row {
  display: flex; align-items: center; gap: 12px; padding: 8px 10px;
  background: var(--bg-2); border: 1px solid var(--border); border-radius: var(--radius-sm);
}
.history-date { flex: 1; font-weight: 500; }
.history-macros { display: flex; gap: 4px; flex-wrap: wrap; }

/* ---------- Modal ---------- */
/* The [hidden] HTML attribute must beat our positioned display rules below. */
[hidden] { display: none !important; }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(2,6,18,.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 50; padding: 20px;
}
.modal {
  background: var(--bg-2); border: 1px solid var(--border);
  border-radius: var(--radius); width: 100%; max-width: 560px;
  max-height: 90vh; display: flex; flex-direction: column;
  box-shadow: var(--shadow);
}
.modal-head, .modal-foot {
  padding: 14px 18px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.modal-foot { border-top: 1px solid var(--border); border-bottom: none; justify-content: flex-end; }
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 18px; overflow: auto; }

.mini-tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); }
.mini-tab {
  background: transparent; color: var(--muted); border: none;
  padding: 8px 12px; border-bottom: 2px solid transparent;
  cursor: pointer; font-size: 14px;
}
.mini-tab.is-active { color: var(--text); border-bottom-color: var(--accent); }

.meal-items { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.meal-item-row {
  display: grid; grid-template-columns: 1fr 200px 32px; gap: 8px; align-items: center;
}
@media (max-width: 600px) { .meal-item-row { grid-template-columns: 1fr; } }
.meal-summary { line-height: 1.7; }

/* ---------- Misc ---------- */
.empty {
  padding: 24px; text-align: center; color: var(--muted);
  background: var(--bg-2); border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
}
.error {
  padding: 12px; background: rgba(239,68,68,.1); color: #fecaca;
  border: 1px solid rgba(239,68,68,.3); border-radius: var(--radius-sm);
}

/* ---------- Plan: week strip ---------- */
.week-strip {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
}
.week-day {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 6px;
  cursor: pointer;
  text-align: center;
  transition: background .12s, border-color .12s, transform .05s;
  display: flex; flex-direction: column; gap: 2px;
  min-width: 0;
}
.week-day:hover { background: #1e2a47; }
.week-day:active { transform: translateY(1px); }
.week-day.is-today { border-color: var(--accent); }
.week-day.is-selected {
  background: linear-gradient(180deg, rgba(56,189,248,.18), rgba(56,189,248,.06));
  border-color: var(--accent);
}
.week-day .wd-name {
  font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em;
}
.week-day .wd-num { font-size: 18px; font-weight: 700; }
.week-day .wd-cal { font-size: 11px; color: var(--cal); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.week-day .wd-meta {
  display: flex; gap: 4px; justify-content: center; align-items: center;
  font-size: 10px; color: var(--muted);
}
.week-day .wd-dot {
  display: inline-block; width: 6px; height: 6px; border-radius: 50%;
  background: var(--ok); opacity: .85;
}
.week-day .wd-dot.partial { background: var(--warn); }
.week-day .wd-dot.empty { background: transparent; border: 1px solid var(--border); }
@media (max-width: 600px) {
  .week-day .wd-num { font-size: 16px; }
  .week-day { padding: 6px 4px; }
}

/* ---------- Today: planned-for-today summary line ---------- */
.plan-summary {
  padding: 8px 10px; background: var(--bg-2);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  margin-bottom: 8px;
}

/* ---------- Plan: slot sections (Breakfast / Lunch / Dinner / Snack) ---------- */
.slot-section { margin-bottom: 14px; }
.slot-section:last-child { margin-bottom: 0; }
.slot-header {
  display: flex; align-items: center; gap: 12px;
  padding: 6px 4px 8px; border-bottom: 1px solid var(--border);
  margin-bottom: 8px;
}
.slot-label {
  font-size: 13px; font-weight: 600; color: var(--text);
  text-transform: uppercase; letter-spacing: .08em;
}
.slot-totals { flex: 1; min-width: 0; }
.slot-items { display: flex; flex-direction: column; gap: 8px; }
@media (max-width: 600px) {
  .slot-header { flex-wrap: wrap; gap: 6px; }
  .slot-totals { flex-basis: 100%; order: 3; }
}

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--border);
  padding: 10px 16px; border-radius: 999px; box-shadow: var(--shadow);
  z-index: 100; font-size: 14px;
}
.toast.ok  { border-color: rgba(34,197,94,.4); color: #bbf7d0; }
.toast.err { border-color: rgba(239,68,68,.4); color: #fecaca; }

/* ---------- Mobile bottom navigation ---------- */
/* Hidden on desktop; the @media (max-width:700px) block flips this on. */
.tabs-bottom { display: none; }
.tabs-bottom .tab {
  flex: 1 1 0;
  min-width: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px;
  padding: 8px 4px;
  border-bottom: none;
  font-size: 11px;
  color: var(--muted);
}
.tabs-bottom .tab .tab-icon { width: 22px; height: 22px; flex-shrink: 0; }
.tabs-bottom .tab .tab-label {
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%;
}
.tabs-bottom .tab.is-active {
  color: var(--accent);
  border-top: 2px solid var(--accent);
  padding-top: 6px;  /* compensate for top border */
}

/* ---------- Mobile layout (≤ 700px) ---------- */
@media (max-width: 700px) {
  /* Header: tighter padding, smaller brand text. Top tab bar is hidden in
     favor of the bottom nav. */
  .app-header { padding: 10px 14px 0; }
  .app-header .tabs-top { display: none; }
  .app-header .header-row { flex-wrap: nowrap; }
  .brand h1 { font-size: 16px; }
  .brand .logo { width: 28px; height: 28px; }
  .user-chip .user-name { display: none; }   /* hide username pill, button is enough */

  .app-main {
    padding: 14px;
    /* leave space for the fixed bottom nav (~64px tall + safe-area) */
    padding-bottom: calc(80px + env(safe-area-inset-bottom));
  }

  /* Show the bottom nav, fixed to the viewport */
  .tabs-bottom {
    display: flex;
    position: fixed; bottom: 0; left: 0; right: 0;
    background: rgba(11, 18, 32, .95);
    backdrop-filter: saturate(180%) blur(12px);
    border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom);
    z-index: 20;
  }

  /* Modal: full-screen sheet on mobile, no rounded corners. */
  .modal-backdrop { padding: 0; align-items: stretch; justify-content: stretch; }
  .modal {
    max-width: none;
    width: 100%;
    max-height: 100vh;
    height: 100dvh;          /* dynamic viewport — survives the URL bar collapse */
    border-radius: 0;
    border: none;
  }
  .modal-body { -webkit-overflow-scrolling: touch; }

  /* Touch targets */
  .btn { min-height: 40px; padding: 10px 14px; }
  .btn.small { min-height: 32px; padding: 6px 12px; font-size: 13px; }
  .icon-btn { width: 40px; height: 40px; }

  /* Inputs at 16px stops iOS Safari from auto-zooming on focus. */
  .input, input[type="text"], input[type="number"],
  input[type="search"], input[type="password"], select { font-size: 16px; }

  /* Tighter view headers */
  .view-head h2 { font-size: 20px; }
  .view-head { margin-bottom: 12px; gap: 8px; }
  .view-head-actions { gap: 4px; }

  /* Panels: less padding, less margin */
  .panel { padding: 14px; margin-bottom: 12px; }

  /* Day-log + plan items: stack actions under macros for thumb access */
  .log-item { padding: 10px; gap: 8px; }

  /* Week strip: tighter so all 7 fit */
  .week-strip { gap: 4px; }
  .week-day { padding: 6px 2px; }
  .week-day .wd-name { font-size: 10px; letter-spacing: .04em; }
  .week-day .wd-num { font-size: 16px; }
  .week-day .wd-cal { font-size: 10px; }
  .week-day .wd-meta { font-size: 9px; }

  /* Auth screen: edge-to-edge with breathing room */
  .auth-card { padding: 22px; max-width: 100%; }

  /* Toast: nudge above the bottom nav */
  .toast { bottom: calc(80px + env(safe-area-inset-bottom)); }
}

/* Smallest phones (≤ 360px): trim further */
@media (max-width: 360px) {
  .app-header { padding-left: 10px; padding-right: 10px; }
  .app-main   { padding-left: 10px; padding-right: 10px; }
  .tabs-bottom .tab { font-size: 10px; padding: 6px 2px; }
  .tabs-bottom .tab .tab-icon { width: 20px; height: 20px; }
  .week-day .wd-num { font-size: 14px; }
}

/* iOS safe-area niceties */
@supports(padding: max(0px)) {
  .app-header { padding-top: max(14px, env(safe-area-inset-top)); }
  body { padding-bottom: env(safe-area-inset-bottom); }
}
