/* กินไรดี — mobile-first, one screen, one big button. */

:root {
  --page: #EFE2D2;
  --bg: #FFF8F0;
  --surface: #FFFFFF;
  --surface-2: #FFF1E3;
  --ink: #2B2117;
  --ink-soft: #7A6A5A;
  --line: #EADCCB;
  --primary: #E8590C;
  --primary-ink: #FFFFFF;
  --green: #2F9E44;
  --yellow: #E8A200;
  --red: #E03131;
  --danger: #C92A2A;
  --radius: 18px;
  --shadow: 0 10px 30px rgba(80, 50, 20, .12);
}

@media (prefers-color-scheme: dark) {
  :root {
    --page: #0C0A07;
    --bg: #16130F;
    --surface: #211C16;
    --surface-2: #2C241C;
    --ink: #F6EFE7;
    --ink-soft: #A99884;
    --line: #3A2F25;
    --primary: #FF7A2F;
    --primary-ink: #1A1209;
    --green: #51CF66;
    --yellow: #FFC93C;
    --red: #FF6B6B;
    --danger: #FF8787;
    --shadow: 0 10px 30px rgba(0, 0, 0, .45);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
}

body {
  background: var(--page);
  color: var(--ink);
  font-family: "IBM Plex Sans Thai", "Noto Sans Thai", -apple-system, BlinkMacSystemFont,
    "Segoe UI", system-ui, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
}

/* One column that the header, the stage and the footer all line up inside —
   without it the header sticks to the window edges on a wide screen while the
   content sits in the middle, and nothing looks related to anything else. */
.app {
  width: 100%;
  max-width: 520px;
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  padding-top: env(safe-area-inset-top);
}

/* ── bars ───────────────────────────────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
}

.brand {
  margin: 0;
  font-size: 1.25rem;
  letter-spacing: -.01em;
}

.foot {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 14px 18px calc(18px + env(safe-area-inset-bottom));
  font-size: .72rem;
  line-height: 1.5;
  color: var(--ink-soft);
  text-align: center;
}

/* ── main stage ─────────────────────────────────────────────────────── */
.stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 12px 18px;
  /* The card clips its overflow, so the stage — not the footer — is what gives
     way when the window is too short to hold everything. */
  min-height: 0;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--line) transparent;
}

.pool {
  margin: 0;
  font-size: .85rem;
  color: var(--ink-soft);
}

.reel-wrap {
  width: 100%;
  max-width: 420px;
  min-height: 84px;
  display: grid;
  place-items: center;
  padding: 10px 14px;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.reel {
  font-size: 1.4rem;
  font-weight: 600;
  text-align: center;
  line-height: 1.35;
  color: var(--ink-soft);
}

.reel.spinning { color: var(--primary); }

.draw {
  width: min(240px, 68vw);
  aspect-ratio: 1;
  border: 0;
  border-radius: 50%;
  background: var(--primary);
  color: var(--primary-ink);
  font: inherit;
  font-size: 1.6rem;
  font-weight: 700;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform .12s ease;
}

.draw:active { transform: scale(.96); }
.draw:disabled { opacity: .6; cursor: default; }

.filter-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 18px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: .95rem;
  cursor: pointer;
}

.count {
  min-width: 22px;
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--primary);
  color: var(--primary-ink);
  font-size: .78rem;
  font-weight: 700;
  text-align: center;
}

.chips-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  max-width: 420px;
}

.mini-chip {
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--ink-soft);
  font-size: .74rem;
}

/* ── buttons ────────────────────────────────────────────────────────── */
button { font-family: inherit; }

.primary, .secondary, .ghost {
  border-radius: 14px;
  font: inherit;
  font-size: 1rem;
  padding: 14px 18px;
  cursor: pointer;
  border: 1px solid transparent;
}

.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); font-weight: 700; }
.secondary { background: var(--surface); color: var(--ink); border-color: var(--line); }
.ghost { background: none; color: var(--ink-soft); padding: 10px 12px; }
.ghost.danger { color: var(--danger); }
.wide { width: 100%; }
.small { padding: 9px 14px; font-size: .9rem; border-radius: 12px; }
.primary:active, .secondary:active, .ghost:active { transform: scale(.98); }

/* ── sheets ─────────────────────────────────────────────────────────── */
.backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(20, 12, 4, .45);
  animation: fade .16s ease;
}

.backdrop[hidden] { display: none; }

.sheet {
  width: 100%;
  max-width: 520px;
  max-height: 92dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  border-radius: 22px 22px 0 0;
  box-shadow: var(--shadow);
  animation: rise .2s ease;
  padding-bottom: env(safe-area-inset-bottom);
}

.sheet.tall { height: 92dvh; }

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 18px 8px;
  border-bottom: 1px solid var(--line);
}

.sheet-head h2 { margin: 0; font-size: 1.05rem; }

.sheet-body {
  flex: 1;
  overflow-y: auto;
  padding: 14px 18px;
  -webkit-overflow-scrolling: touch;
}

.sheet-body.center { display: grid; place-items: center; }
.sheet-body h3 { margin: 16px 0 8px; font-size: .85rem; color: var(--ink-soft); font-weight: 600; }
.sheet-body h3:first-child { margin-top: 0; }

.sheet-foot {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px 18px calc(16px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
}

@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(16px); opacity: 0; } }

@media (prefers-reduced-motion: reduce) {
  .backdrop, .sheet { animation: none; }
  .draw { transition: none; }
}

/* ── result ─────────────────────────────────────────────────────────── */
.result-sheet { text-align: center; padding: 22px 18px calc(18px + env(safe-area-inset-bottom)); gap: 12px; }

.result-badge {
  align-self: center;
  padding: 5px 14px;
  border-radius: 999px;
  font-size: .8rem;
  font-weight: 700;
  color: #fff;
}

.result-badge[data-key="green"]  { background: var(--green); }
.result-badge[data-key="yellow"] { background: var(--yellow); color: #3B2A00; }
.result-badge[data-key="red"]    { background: var(--red); }
.result-badge[data-key="none"]   { background: var(--surface-2); color: var(--ink-soft); }

.result-name { margin: 0; font-size: 1.85rem; line-height: 1.25; }
.result-meta { margin: 0; color: var(--ink-soft); font-size: .9rem; }

.nutri {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 4px;
}

.nutri-cell {
  padding: 12px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
}

.nutri-cell b { display: block; font-size: 1.5rem; }
.nutri-cell span { font-size: .75rem; color: var(--ink-soft); }

.prov {
  align-self: center;
  border: 0;
  background: none;
  color: var(--ink-soft);
  font: inherit;
  font-size: .74rem;
  text-decoration: underline dotted;
  cursor: pointer;
  padding: 2px 6px;
}

.result-tags { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 6px; }

/* ── chips ──────────────────────────────────────────────────────────── */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }

.chip {
  padding: 9px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: .88rem;
  cursor: pointer;
}

.chip[aria-pressed="true"] {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--primary-ink);
  font-weight: 600;
}

.note { margin: 12px 0 0; font-size: .78rem; color: var(--ink-soft); line-height: 1.6; }

/* ── forms ──────────────────────────────────────────────────────────── */
.field { display: block; margin-bottom: 14px; }
.field > span { display: block; margin-bottom: 6px; font-size: .82rem; color: var(--ink-soft); }

.field input, .field select, .search {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: 1rem;
}

.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.manage-tools { display: flex; gap: 8px; align-items: center; }
.manage-tools .search { flex: 1; }

.borrow {
  margin: -4px 0 14px;
  padding: 12px 14px;
  border: 1px solid var(--primary);
  border-radius: 14px;
  background: var(--surface-2);
}

.borrow-q { margin: 0 0 10px; font-size: .88rem; line-height: 1.5; }
.borrow-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ── meal list ──────────────────────────────────────────────────────── */
.meal-list { list-style: none; margin: 12px 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }

.meal-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.meal-row.is-hidden { opacity: .45; }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: 0 0 auto; }
.dot[data-key="green"] { background: var(--green); }
.dot[data-key="yellow"] { background: var(--yellow); }
.dot[data-key="red"] { background: var(--red); }
.dot[data-key="none"] { background: var(--line); }

.meal-main { flex: 1; min-width: 0; }
.meal-name { display: block; font-size: .95rem; }
.meal-sub { display: block; font-size: .74rem; color: var(--ink-soft); margin-top: 2px; }
.meal-flag { font-size: .7rem; color: var(--ink-soft); flex: 0 0 auto; }

/* ── share ──────────────────────────────────────────────────────────── */
.share-preview {
  width: 100%;
  max-width: 260px;
  height: auto;
  border-radius: 16px;
  box-shadow: var(--shadow);
}

/* ── toast ──────────────────────────────────────────────────────────── */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 40;
  max-width: 88vw;
  padding: 11px 18px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--bg);
  font-size: .85rem;
  box-shadow: var(--shadow);
}

.toast[hidden] { display: none; }


/* ── desktop ─────────────────────────────────────────────────────────
   Phone-shaped app in a browser window: give the column a real edge and
   centre it, rather than letting one thin strip float in a wide void. */
@media (min-width: 700px) {
  body {
    justify-content: center;
    padding: 28px 0;
  }

  .app {
    flex: none;
    height: min(820px, calc(100dvh - 56px));
    border: 1px solid var(--line);
    border-radius: 28px;
    box-shadow: var(--shadow);
    overflow: hidden;
  }

  .topbar { padding: 20px 26px 16px; }
  .brand { font-size: 1.35rem; }
  .stage { gap: 22px; }
  .draw { width: 260px; font-size: 1.7rem; }
  .foot { padding: 16px 26px 22px; }

  /* bottom sheets make no sense when there is no thumb — centre them */
  .backdrop { align-items: center; }
  .sheet {
    border-radius: 22px;
    max-height: min(86dvh, 780px);
  }
  .sheet.tall { height: min(86dvh, 780px); }
}

/* A short desktop window (a laptop with the browser chrome eating the height)
   should shrink the button, not scroll it. */
@media (min-width: 700px) and (max-height: 800px) {
  .draw { width: 200px; font-size: 1.4rem; }
  .reel-wrap { min-height: 68px; }
  .reel { font-size: 1.2rem; }
  .stage { gap: 16px; }
}
