@import url('https://fonts.googleapis.com/css2?family=Fraunces:ital,opsz,wght@0,9..144,500;0,9..144,600;1,9..144,500&family=IBM+Plex+Sans+Thai:wght@300;400;500;600;700&display=swap');

  :root {
    /* warm specialty-café palette */
    --bg: #f4ede1;
    --bg-2: #efe5d5;
    --card: #fffcf6;
    --card-2: #fbf5ea;
    --ink: #2a2018;
    --ink-soft: #5c4f42;
    --muted: #9c8d79;
    --line: #e9ddc9;
    --brand: #6b4a2f;          /* espresso */
    --brand-deep: #3a2718;
    --crema: #c5853c;          /* caramel accent */
    --crema-soft: #f3e3cc;
    --accent: #3f7d5f;         /* paid green */
    --accent-soft: #e1efe6;
    --warn: #bd4a30;           /* unpaid terracotta */
    --warn-soft: #f6e0d8;

    --font-body: 'IBM Plex Sans Thai', -apple-system, 'Segoe UI', sans-serif;
    --font-display: 'Fraunces', Georgia, serif;

    --radius: 18px;
    --radius-sm: 12px;
    --tabh: 74px;
    --shadow-sm: 0 1px 2px rgba(58,39,24,.06);
    --shadow: 0 6px 22px -10px rgba(58,39,24,.28);
    --shadow-lg: 0 18px 48px -18px rgba(58,39,24,.42);
    --ease: cubic-bezier(.22,.61,.36,1);
  }

  * { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
  html, body { margin: 0; padding: 0; }
  body {
    font-family: var(--font-body);
    color: var(--ink);
    font-size: 16px;
    line-height: 1.5;
    letter-spacing: .1px;
    background:
      radial-gradient(1200px 600px at 12% -8%, #fbf3e6 0%, transparent 55%),
      radial-gradient(900px 500px at 110% 4%, #f7ecdb 0%, transparent 50%),
      linear-gradient(180deg, var(--bg) 0%, var(--bg-2) 100%);
    background-attachment: fixed;
    min-height: 100vh;
  }
  /* subtle paper grain */
  body::before {
    content: ''; position: fixed; inset: 0; pointer-events: none; z-index: 0; opacity: .5;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='120' height='120'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='.035'/%3E%3C/svg%3E");
  }
  #app { position: relative; z-index: 1; }
  .hidden { display: none !important; }

  /* ---------- splash ---------- */
  .splash { min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; padding: 0 28px; text-align: center; }
  .splash-logo {
    font-family: var(--font-display); font-style: italic; font-weight: 600;
    font-size: 40px; color: var(--brand-deep); letter-spacing: .3px;
    animation: rise .7s var(--ease) both;
  }
  .splash-msg { color: var(--ink-soft); animation: rise .7s .08s var(--ease) both; max-width: 360px; }

  @keyframes rise { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }
  @keyframes pop { from { opacity: 0; transform: scale(.96) translateY(8px); } to { opacity: 1; transform: none; } }
  @keyframes fade { from { opacity: 0; } to { opacity: 1; } }

  main { padding: 18px 16px calc(var(--tabh) + 34px); max-width: 580px; margin: 0 auto; }
  /* add page needs extra room so the last fields clear the fixed save bar */
  #page-add { padding-bottom: 120px; }
  .page { display: none; }
  /* opacity-only (no transform) so fixed children like .savebar stay viewport-anchored */
  .page.active { display: block; animation: fade .4s var(--ease) both; }

  h2 {
    font-size: 18px; font-weight: 600; margin: 24px 0 10px; color: var(--ink);
    display: flex; align-items: center; gap: 10px;
  }
  h2:first-child { margin-top: 6px; }
  h2::before { content: ''; width: 18px; height: 3px; border-radius: 3px; background: var(--crema); }

  .step-label {
    font-size: 12px; color: var(--muted); margin: 22px 0 8px; font-weight: 600;
    text-transform: uppercase; letter-spacing: 1.2px;
  }

  /* ---------- chips ---------- */
  .chips { display: flex; flex-wrap: wrap; gap: 8px; }
  .chip {
    font-family: var(--font-body); font-size: 15px; font-weight: 500;
    border: 1.5px solid var(--line); background: var(--card); color: var(--ink-soft);
    border-radius: 999px; padding: 9px 16px; cursor: pointer; box-shadow: var(--shadow-sm);
    transition: transform .12s var(--ease), background .15s, border-color .15s, color .15s, box-shadow .15s;
    user-select: none;
  }
  .chip:hover { border-color: var(--crema); }
  .chip:active { transform: scale(.94); }
  .chip.sel {
    background: linear-gradient(165deg, var(--brand) 0%, var(--brand-deep) 100%);
    border-color: var(--brand-deep); color: #fdf6ea;
    box-shadow: 0 6px 16px -8px rgba(58,39,24,.6);
  }
  .chip.ghost { border-style: dashed; color: var(--muted); background: transparent; box-shadow: none; }
  .chip.ghost:hover { color: var(--crema); }

  /* ---------- repeat order (Phase 2) ---------- */
  .repeat-order { margin: 4px 0 8px; animation: pop .35s var(--ease) both; }
  .repeat-btn {
    width: 100%; font-family: var(--font-body); font-size: 15px; font-weight: 600;
    border: 1.5px solid var(--crema); background: linear-gradient(165deg, var(--crema-soft) 0%, #f8edd8 100%);
    color: var(--brand-deep); border-radius: var(--radius-sm); padding: 13px 16px; cursor: pointer;
    box-shadow: var(--shadow-sm); transition: transform .12s var(--ease), box-shadow .15s;
    text-align: left;
  }
  .repeat-btn:hover { box-shadow: var(--shadow); }
  .repeat-btn:active { transform: scale(.99); }

  /* ---------- menu list ---------- */
  #menuList { display: flex; flex-direction: column; gap: 8px; }
  .menu-item {
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    background: var(--card); border: 1.5px solid var(--line); border-radius: var(--radius-sm);
    padding: 14px 16px; cursor: pointer; box-shadow: var(--shadow-sm);
    transition: transform .12s var(--ease), border-color .15s, background .15s, box-shadow .15s;
  }
  .menu-item:hover { border-color: var(--crema); }
  .menu-item:active { transform: scale(.99); }
  .menu-item.sel {
    border-color: var(--brand); background: var(--card-2);
    box-shadow: inset 3px 0 0 var(--crema), var(--shadow);
  }
  .menu-item .price {
    font-family: var(--font-display); font-weight: 600; font-size: 19px; color: var(--brand);
    font-variant-numeric: tabular-nums;
  }
  .menu-item.fav { border-color: #e8d4a8; background: linear-gradient(165deg, #fffdf8 0%, var(--card) 100%); }
  .menu-item .fav-star { color: var(--crema); margin-right: 4px; }

  .opts { display: flex; flex-wrap: wrap; gap: 8px; }

  /* ---------- inputs / stepper ---------- */
  .row { display: flex; align-items: center; gap: 12px; }
  .stepper { display: flex; align-items: center; gap: 16px; }
  .stepper button {
    width: 46px; height: 46px; border-radius: 14px; border: 1.5px solid var(--line);
    background: var(--card); font-size: 24px; color: var(--brand); cursor: pointer;
    box-shadow: var(--shadow-sm); transition: transform .1s var(--ease), border-color .15s;
    display: flex; align-items: center; justify-content: center;
  }
  .stepper button:hover { border-color: var(--crema); }
  .stepper button:active { transform: scale(.9); }
  .stepper .val { min-width: 34px; text-align: center; font-family: var(--font-display); font-size: 24px; font-weight: 600; }

  textarea, input[type=text], input[type=date] {
    width: 100%; border: 1.5px solid var(--line); border-radius: var(--radius-sm);
    padding: 12px 14px; font: inherit; background: var(--card); color: var(--ink);
    resize: vertical; box-shadow: var(--shadow-sm); transition: border-color .15s, box-shadow .15s;
  }
  textarea:focus, input:focus { outline: none; border-color: var(--crema); box-shadow: 0 0 0 4px var(--crema-soft); }
  input[type=date] { width: auto; }

  .pay { display: flex; flex-wrap: wrap; gap: 8px; }

  /* ---------- sticky save bar ---------- */
  .savebar {
    position: fixed; left: 0; right: 0; bottom: calc(var(--tabh) + 18px);
    display: flex; gap: 10px; padding: 14px 16px;
    max-width: 580px; margin: 0 auto; z-index: 30;
    background: var(--bg-2);
    border: 1.5px solid var(--line); border-bottom: none;
    border-radius: 20px 20px 0 0;
    box-shadow: 0 -12px 28px -14px rgba(58,39,24,.22);
  }
  .btn {
    flex: 1; border: none; border-radius: 15px; padding: 16px; font-family: var(--font-body);
    font-size: 15px; font-weight: 600; cursor: pointer; box-shadow: var(--shadow);
    transition: transform .12s var(--ease), box-shadow .15s, opacity .15s; letter-spacing: .2px;
  }
  .btn:active { transform: translateY(1px) scale(.99); }
  .btn.secondary { background: var(--card); color: var(--brand); border: 1.5px solid var(--line); box-shadow: var(--shadow-sm); }
  .btn.primary { background: linear-gradient(165deg, var(--crema) 0%, #ab6f2b 100%); color: #fff; }
  .btn.primary:hover { box-shadow: 0 10px 26px -10px rgba(176,106,40,.7); }
  .btn:disabled { opacity: .4; box-shadow: none; cursor: default; transform: none; }

  /* ---------- summary card ---------- */
  .summary {
    position: relative; overflow: hidden;
    background: linear-gradient(150deg, var(--card) 0%, var(--card-2) 100%);
    border: 1.5px solid var(--line); border-radius: var(--radius);
    padding: 18px; margin-bottom: 14px; box-shadow: var(--shadow); animation: pop .45s var(--ease) both;
  }
  .summary::after {
    content: ''; position: absolute; top: -40px; right: -40px; width: 130px; height: 130px;
    background: radial-gradient(circle, var(--crema-soft), transparent 70%); pointer-events: none;
  }
  .summary .title { font-family: var(--font-display); font-weight: 600; font-size: 21px; margin-bottom: 12px; color: var(--brand-deep); }
  .summary .grid { display: grid; grid-template-columns: 1fr auto; gap: 9px 12px; font-size: 14px; color: var(--ink-soft); }
  .summary .grid .v { text-align: right; font-weight: 600; color: var(--ink); font-variant-numeric: tabular-nums; }
  .summary .v.warn { color: var(--warn); }
  .summary .v.ok { color: var(--accent); }
  .summary .sub { grid-column: 1 / -1; }
  .summary-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
  .summary-actions .chip { font-size: 13px; padding: 8px 13px; }

  /* ---------- order card (chat view) ---------- */
  #orderList { display: flex; flex-direction: column; gap: 10px; }
  .order {
    background: var(--card); border: 1.5px solid var(--line); border-radius: var(--radius);
    padding: 14px 16px; box-shadow: var(--shadow-sm); animation: pop .35s var(--ease) both;
    transition: opacity .2s, border-color .2s;
  }
  .order.done { opacity: .5; }
  .order .head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
  .order .who { font-weight: 600; font-size: 16px; }
  .order .time { color: var(--muted); font-size: 12px; white-space: nowrap; }
  .order .body { margin: 5px 0; color: var(--ink); }
  .order .sub { color: var(--ink-soft); font-size: 13px; background: var(--card-2); border-radius: 9px; padding: 6px 10px; margin-top: 6px; }
  .order .foot { display: flex; align-items: center; justify-content: space-between; gap: 10px 14px; margin-top: 12px; flex-wrap: wrap; }
  .order .pay-tag { font-size: 13px; font-weight: 600; padding: 3px 10px; border-radius: 999px; }
  .pay-unpaid { color: var(--warn); background: var(--warn-soft); }
  .pay-paid { color: var(--accent); background: var(--accent-soft); }
  .pay-free { color: var(--muted); background: var(--card-2); }
  .order .foot > div:first-child { display: flex; align-items: center; gap: 8px; font-variant-numeric: tabular-nums; white-space: nowrap; }
  .order .net-amt { font-weight: 600; }
  .order .acts { display: flex; gap: 6px; }
  .order .acts button {
    border: 1.5px solid var(--line); background: var(--card); border-radius: 10px;
    padding: 7px 12px; font: inherit; font-size: 13px; font-weight: 500; color: var(--ink-soft);
    cursor: pointer; transition: transform .1s var(--ease), border-color .15s, color .15s;
  }
  .order .acts button:hover { border-color: var(--crema); }
  .order .acts button:active { transform: scale(.93); }
  .order .acts .del { color: var(--warn); }
  .order.pending { border-style: dashed; opacity: .8; }
  .order.error { border-color: var(--warn); background: var(--warn-soft); }
  .badge { font-size: 11px; padding: 2px 8px; border-radius: 7px; background: var(--crema-soft); color: var(--brand); }

  .empty { text-align: center; color: var(--muted); padding: 48px 14px; font-style: italic; white-space: pre-line; }

  /* ---------- round view switcher ---------- */
  .viewbar { display: flex; align-items: center; gap: 8px; margin-bottom: 14px; flex-wrap: wrap; }
  .viewbar .chips { flex: 1; }
  #btnUnpaidOnly { white-space: nowrap; }
  #btnUnpaidOnly.sel { background: var(--warn); border-color: var(--warn); color: #fff; box-shadow: 0 6px 16px -8px rgba(189,74,48,.6); }

  /* ---------- grouped views ---------- */
  .group { background: var(--card); border: 1.5px solid var(--line); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 10px; box-shadow: var(--shadow-sm); animation: pop .35s var(--ease) both; }
  #orderList .group { margin-bottom: 0; }
  .group .g-head { display: flex; justify-content: space-between; gap: 10px; font-weight: 600; margin-bottom: 8px; font-size: 16px; }
  .group .g-head span:last-child { color: var(--brand); font-variant-numeric: tabular-nums; }
  .group .g-line { color: var(--ink-soft); font-size: 14px; padding: 3px 0; }

  /* ---------- prep summary (สรุปชง) ---------- */
  .prep-summary {
    background: var(--card); border: 1.5px solid var(--line); border-radius: var(--radius);
    padding: 18px 16px; box-shadow: var(--shadow); animation: pop .4s var(--ease) both;
    font-size: 16px; line-height: 1.55;
  }
  .prep-title {
    font-family: var(--font-display); font-weight: 600; font-size: 22px;
    color: var(--brand-deep); margin-bottom: 14px;
  }
  .prep-line {
    display: flex; justify-content: space-between; gap: 12px;
    padding: 5px 0; color: var(--ink);
  }
  .prep-line .prep-qty {
    font-weight: 600; color: var(--brand); font-variant-numeric: tabular-nums; white-space: nowrap;
  }
  .prep-subtotal {
    margin-top: 8px; padding-top: 8px; border-top: 1px dashed var(--line);
    font-weight: 600; color: var(--brand-deep);
  }
  .prep-sep {
    margin: 14px 0 10px; color: var(--muted); font-size: 14px; letter-spacing: 2px;
    text-align: center;
  }
  .prep-people {
    margin-top: 14px; background: var(--card); border: 1.5px solid var(--line);
    border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow-sm);
    animation: pop .45s .06s var(--ease) both;
  }
  .prep-person-line {
    padding: 6px 0; color: var(--ink-soft); font-size: 15px; line-height: 1.5;
    border-bottom: 1px solid var(--line);
  }
  .prep-person-line:last-child { border-bottom: none; padding-bottom: 0; }
  .prep-person-line:first-child { padding-top: 0; }

  /* ---------- dashboard ---------- */
  #rangeChips { gap: 8px; }
  .rank {
    display: flex; align-items: center; gap: 12px; background: var(--card);
    border: 1.5px solid var(--line); border-radius: var(--radius-sm); padding: 12px 14px;
    margin-bottom: 7px; box-shadow: var(--shadow-sm); animation: pop .3s var(--ease) both;
  }
  .rank .n { width: 26px; height: 26px; display: flex; align-items: center; justify-content: center; font-family: var(--font-display); font-weight: 600; color: var(--brand); background: var(--crema-soft); border-radius: 8px; font-size: 14px; }
  .rank .name { flex: 1; min-width: 0; }
  .rank .name > .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
  .rank .val { color: var(--ink-soft); font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
  .rank .val.warn { color: var(--warn); }
  .barline { height: 7px; border-radius: 6px; background: var(--crema-soft); overflow: hidden; margin-top: 7px; }
  .barline > span { display: block; height: 100%; background: linear-gradient(90deg, var(--crema), #ab6f2b); border-radius: 6px; transition: width .5s var(--ease); }
  .sub { font-size: 13px; color: var(--muted); }

  /* ---------- tab bar ---------- */
  .tabbar {
    position: fixed; bottom: 14px; left: 50%; transform: translateX(-50%);
    display: flex; gap: 4px; padding: 6px;
    background: rgba(255,252,246,.86); backdrop-filter: blur(14px) saturate(1.4);
    border: 1.5px solid var(--line); border-radius: 999px; box-shadow: var(--shadow-lg);
    width: calc(100% - 32px); max-width: 420px; z-index: 40;
  }
  .tab {
    flex: 1; border: none; background: none; cursor: pointer; border-radius: 999px;
    padding: 9px 6px; font-family: var(--font-body); font-size: 19px; color: var(--muted);
    display: flex; flex-direction: column; align-items: center; gap: 1px;
    transition: color .18s, background .18s, transform .12s var(--ease);
  }
  .tab span { font-size: 11px; font-weight: 600; }
  .tab:active { transform: scale(.94); }
  .tab.active { color: var(--brand-deep); background: var(--crema-soft); }

  /* ---------- toast ---------- */
  .toast {
    position: fixed; bottom: calc(var(--tabh) + 90px); left: 50%; transform: translateX(-50%);
    background: var(--brand-deep); color: #fdf6ea; padding: 12px 20px; border-radius: 999px;
    font-size: 14px; font-weight: 500; z-index: 60; box-shadow: var(--shadow-lg);
    animation: pop .25s var(--ease) both; max-width: 90vw; text-align: center;
  }

  /* staggered entrance for lists */
  .order:nth-child(1), .rank:nth-child(1), .group:nth-child(1) { animation-delay: .02s; }
  .order:nth-child(2), .rank:nth-child(2), .group:nth-child(2) { animation-delay: .06s; }
  .order:nth-child(3), .rank:nth-child(3), .group:nth-child(3) { animation-delay: .10s; }
  .order:nth-child(4), .rank:nth-child(4), .group:nth-child(4) { animation-delay: .14s; }
  .order:nth-child(5), .rank:nth-child(5), .group:nth-child(5) { animation-delay: .18s; }
  .order:nth-child(n+6), .rank:nth-child(n+6), .group:nth-child(n+6) { animation-delay: .22s; }

  @media (prefers-reduced-motion: reduce) {
    *, *::before { animation: none !important; transition: none !important; }
  }