:root {
  --ink: #111827;
  --muted: #64748b;
  --surface: #ffffff;
  --line: #e5e7eb;
  --bg: #f4f6f8;
  --orange: #ff6b21;
  --green: #16a36a;
  --shadow: 0 18px 50px rgba(15, 23, 42, .08);
}

* { box-sizing: border-box; }
html { min-height: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: var(--bg);
  font-family: "Noto Sans TC", system-ui, sans-serif;
}
button, input { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; }

.topbar {
  height: 78px;
  padding: 0 clamp(20px, 4vw, 64px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 28px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; color: inherit; text-decoration: none; }
.brand > span { margin-left: 12px; }
.brand-logo { width: 108px; height: 56px; flex: 0 0 auto; object-fit: contain; }
.brand strong, .brand small { display: block; line-height: 1.25; }
.brand small { color: var(--muted); font-size: 11px; margin-top: 2px; letter-spacing: .08em; }
.view-switcher { padding: 5px; background: #f1f5f9; border-radius: 12px; display: flex; }
.view-switcher a + a { margin-left: 4px; }
.view-switcher a { padding: 8px 14px; color: var(--muted); text-decoration: none; font-size: 14px; font-weight: 700; border-radius: 9px; }
.view-switcher a.active { color: var(--ink); background: #fff; box-shadow: 0 2px 8px rgba(15,23,42,.08); }
.settings-link {
  width: 40px;
  height: 40px;
  display: none;
  place-items: center;
  margin-left: auto;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
  text-decoration: none;
  transition: color .2s, border-color .2s, background .2s;
}
.settings-link:hover, .settings-link:focus-visible { color: var(--ink); border-color: #cbd5e1; background: #f8fafc; }
.settings-icon { width: 21px; height: 21px; fill: currentColor; }
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
body[data-view="tv"] .view-switcher { display: none; }
body[data-view="tv"] .settings-link { display: grid; }
.clock { min-width: 84px; text-align: right; font-weight: 800; font-variant-numeric: tabular-nums; }
.view { width: min(1480px, 100%); margin: 0 auto; padding: clamp(28px, 4vw, 56px) clamp(18px, 4vw, 64px); }
.eyebrow { margin: 0 0 8px; color: var(--orange); font-size: 12px; font-weight: 900; letter-spacing: .17em; }
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: clamp(28px, 3vw, 46px); letter-spacing: -.04em; }
.hero-row { display: flex; align-items: end; justify-content: space-between; gap: 20px; margin-bottom: 34px; }
.live-pill { padding: 9px 13px; color: var(--green); background: #eaf9f2; border-radius: 999px; font-size: 13px; font-weight: 800; white-space: nowrap; }
.live-pill span { width: 8px; height: 8px; display: inline-block; margin-right: 5px; background: currentColor; border-radius: 50%; animation: pulse 1.5s infinite; }
@keyframes pulse { 50% { opacity: .3; } }

.tv-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 22px; }
.stall-card { position: relative; min-height: 280px; padding: 28px; overflow: hidden; background: #fff; border: 1px solid var(--line); border-radius: 22px; box-shadow: var(--shadow); }
.stall-card::before { content: ""; position: absolute; inset: 0 auto 0 0; width: 7px; background: var(--accent); }
.stall-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.stall-id { color: var(--muted); font-size: 12px; font-weight: 800; letter-spacing: .12em; }
.stall-head h2 { margin: 5px 0 0; font-size: clamp(20px, 2vw, 28px); }
.status-dot { padding: 6px 10px; color: var(--accent); background: #f8fafc; border: 1px solid #e2e8f0; border-radius: 999px; font-size: 12px; font-weight: 800; }
.number-list { margin-top: 25px; display: flex; flex-wrap: wrap; gap: 12px; }
.pickup-number { min-width: 108px; padding: 12px 18px; color: var(--ink); background: #f8fafc; border: 1px solid #e2e8f0; border-radius: 14px; text-align: center; font-size: clamp(28px, 3.2vw, 52px); font-weight: 900; letter-spacing: .02em; font-variant-numeric: tabular-nums; animation: pop .25s ease-out; }
@keyframes pop { from { opacity: 0; transform: scale(.88); } }
.empty-state { width: 100%; padding: 40px 10px; color: #94a3b8; text-align: center; }
.empty-state strong { display: block; color: #64748b; font-size: 18px; margin-bottom: 6px; }
.tv-note { margin: 28px 0 0; color: var(--muted); text-align: center; font-size: 14px; }

.kitchen-view { max-width: 1100px; }
.kitchen-heading { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 28px; }
.kitchen-heading > div > p:last-child { margin: 12px 0 0; color: var(--muted); }
.kitchen-actions { display: flex; }
.kitchen-actions button + button { margin-left: 8px; }
.ghost-btn { padding: 10px 15px; border: 1px solid var(--line); border-radius: 10px; background: #fff; font-weight: 700; }
.stall-tabs { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-bottom: 16px; }
.stall-tab { padding: 15px 10px; border: 1px solid var(--line); border-radius: 12px; color: var(--muted); background: #fff; font-weight: 800; }
.stall-tab.active { color: white; border-color: var(--tab-color); background: var(--tab-color); box-shadow: 0 8px 20px rgba(15, 23, 42, .18); }
.console-card { padding: clamp(22px, 4vw, 38px); background: #fff; border: 1px solid var(--line); border-radius: 22px; box-shadow: var(--shadow); }
.console-top { display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid var(--line); padding-bottom: 20px; }
.stall-label { color: var(--muted); font-size: 12px; font-weight: 700; }
.console-top h2 { margin: 4px 0 0; font-size: 28px; }
.stall-name-row { display: flex; align-items: center; }
.stall-name-row .rename-btn { margin-left: 10px; }
.rename-btn { padding: 5px 9px; color: var(--muted); background: #f8fafc; border: 1px solid var(--line); border-radius: 8px; font-size: 12px; font-weight: 700; }
.rename-btn:hover { color: var(--orange); border-color: #fdba8c; background: #fff7ed; }
.count-badge { padding: 7px 12px; border-radius: 999px; background: #fff2eb; color: #d94b0b; font-size: 13px; font-weight: 800; }
.order-form { margin: 26px 0 34px; }
.order-form label { display: block; margin-bottom: 9px; font-size: 14px; font-weight: 800; }
.input-row { display: flex; }
.input-row .primary-btn { margin-left: 10px; }
.input-row input { min-width: 0; flex: 1; padding: 15px 17px; border: 2px solid #dbe2ea; border-radius: 12px; outline: none; text-transform: uppercase; font-size: 20px; font-weight: 800; }
.input-row input:focus { border-color: var(--orange); box-shadow: 0 0 0 4px #ff6b2118; }
.primary-btn { padding: 0 24px; border: 0; border-radius: 12px; color: #fff; background: var(--orange); font-weight: 900; }
.order-form small { display: block; margin-top: 8px; color: var(--muted); }
.section-title { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.section-title h3 { margin: 0; font-size: 16px; }
.text-btn { padding: 5px; color: var(--muted); border: 0; background: none; font-size: 13px; font-weight: 700; }
.kitchen-orders { min-height: 100px; margin-top: 13px; display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; }
.order-chip { position: relative; min-height: 82px; border: 2px solid #e2e8f0; border-radius: 14px; color: var(--ink); background: #f8fafc; font-size: 27px; font-weight: 900; }
.order-chip:hover { color: #b42318; border-color: #fda29b; background: #fff5f4; }
.order-chip::after { content: "點擊完成"; display: block; margin-top: 2px; color: var(--muted); font-size: 10px; font-weight: 600; }
.mini-empty { grid-column: 1 / -1; padding: 30px; color: #94a3b8; background: #f8fafc; border: 1px dashed #cbd5e1; border-radius: 13px; text-align: center; }
.history-section { margin-top: 30px; padding-top: 22px; border-top: 1px solid var(--line); }
.history-list { margin-top: 12px; display: flex; flex-wrap: wrap; gap: 8px; }
.history-item { padding: 7px 10px; color: #64748b; background: #f1f5f9; border-radius: 8px; font-size: 13px; text-decoration: line-through; }
.toast { position: fixed; z-index: 10; left: 50%; bottom: 25px; padding: 11px 18px; color: #fff; background: #111827; border-radius: 10px; box-shadow: 0 12px 30px #0003; opacity: 0; pointer-events: none; transform: translate(-50%, 10px); transition: .2s; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.pin-overlay {
  position: fixed;
  z-index: 30;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(15, 23, 42, .56);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
}
.pin-overlay[hidden] { display: none; }
.pin-card {
  width: min(390px, 100%);
  padding: 32px;
  border: 1px solid #dbe2ea;
  border-radius: 22px;
  background: #fff;
  box-shadow: 0 28px 80px rgba(15, 23, 42, .28);
  text-align: center;
}
.pin-lock-icon { width: 42px; height: 42px; fill: var(--orange); }
.pin-eyebrow { margin: 12px 0 5px; color: var(--orange); font-size: 10px; font-weight: 900; letter-spacing: .18em; }
.pin-card h2 { margin: 0; font-size: 24px; }
.pin-copy { margin: 8px 0 20px; color: var(--muted); font-size: 13px; }
.pin-stall-label { margin: 18px 0 8px; color: var(--ink); font-size: 13px; font-weight: 800; }
.pin-stall-picker { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin-bottom: 12px; }
.pin-stall-choice { padding: 10px 8px; color: var(--muted); border: 1px solid #dbe2ea; border-radius: 10px; background: #f8fafc; font-size: 12px; font-weight: 800; }
.pin-stall-choice.active { color: #fff; border-color: var(--choice-color); background: var(--choice-color); }
.pin-input {
  width: 100%;
  padding: 13px 16px;
  border: 2px solid #dbe2ea;
  border-radius: 12px;
  outline: none;
  text-align: center;
  font-size: 28px;
  font-weight: 900;
  letter-spacing: .55em;
}
.pin-input:focus { border-color: var(--orange); box-shadow: 0 0 0 4px #ff6b2118; }
.pin-error { min-height: 20px; margin: 7px 0 5px; color: #b42318; font-size: 12px; }
.pin-submit { width: 100%; min-height: 46px; }
.pin-submit:disabled { opacity: .6; cursor: wait; }
.pin-back { display: inline-block; margin-top: 14px; color: var(--muted); font-size: 12px; text-decoration: none; }
.pin-links { display: flex; justify-content: center; }
.pin-links .pin-back + .pin-back { margin-left: 18px; }
body.pin-locked { overflow: hidden; }

.admin-view { max-width: 1100px; }
.admin-heading { display: flex; align-items: center; justify-content: space-between; margin-bottom: 24px; }
.admin-heading > div > p:last-child { margin: 10px 0 0; color: var(--muted); }
.ghost-link { padding: 10px 15px; color: var(--ink); border: 1px solid var(--line); border-radius: 10px; background: #fff; font-weight: 700; text-decoration: none; }
.admin-login-card {
  width: min(460px, 100%);
  margin: 60px auto 0;
  padding: 30px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: #fff;
  box-shadow: var(--shadow);
}
.admin-login-card h2 { margin-bottom: 6px; }
.admin-login-card form > p:not(.admin-error) { margin-bottom: 20px; color: var(--muted); }
.admin-login-card label, .admin-pin-card label { display: block; margin-bottom: 7px; font-size: 13px; font-weight: 800; }
.admin-login-card input, .admin-pin-card input {
  width: 100%;
  padding: 12px 14px;
  border: 2px solid #dbe2ea;
  border-radius: 11px;
  outline: none;
  font-size: 18px;
  font-weight: 800;
}
.admin-login-card input:focus, .admin-pin-card input:focus { border-color: var(--orange); box-shadow: 0 0 0 4px #ff6b2118; }
.admin-error { min-height: 20px; margin: 6px 0; color: #b42318; font-size: 12px; }
.admin-primary { width: 100%; min-height: 46px; }
.admin-primary:disabled { opacity: .6; cursor: wait; }
.admin-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  padding: 16px 18px;
  border: 1px solid #bbf7d0;
  border-radius: 14px;
  background: #f0fdf4;
}
.admin-toolbar strong, .admin-toolbar span { display: block; }
.admin-toolbar span { margin-top: 3px; color: var(--muted); font-size: 12px; }
.admin-pin-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.admin-pin-card { padding: 22px; border: 1px solid var(--line); border-radius: 16px; background: #fff; box-shadow: 0 8px 24px rgba(15, 23, 42, .06); }
.admin-pin-card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; }
.admin-pin-card-head h2 { margin: 0; font-size: 20px; }
.admin-stall-number { color: var(--muted); font-size: 11px; font-weight: 800; letter-spacing: .1em; }
.admin-pin-fields { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.admin-pin-card .admin-primary { margin-top: 14px; }
.admin-pin-status { min-height: 20px; margin: 7px 0 0; color: var(--green); font-size: 12px; }
.admin-pin-status.error { color: #b42318; }

@media (min-width: 1500px) { .tv-grid { grid-template-columns: repeat(4, 1fr); } .stall-card { min-height: 420px; } }
@media (max-width: 720px) {
  .topbar { height: auto; padding: 12px 16px; flex-wrap: wrap; }
  .brand-logo { width: 88px; height: 46px; }
  .view-switcher { order: 3; width: 100%; }
  .view-switcher a { flex: 1; text-align: center; }
  .clock { min-width: 0; }
  .tv-grid { grid-template-columns: 1fr; }
  .stall-card { min-height: 225px; }
  .hero-row { align-items: start; }
  .kitchen-heading { align-items: start; }
  .stall-tabs { grid-template-columns: repeat(2, 1fr); }
  .input-row { flex-direction: column; }
  .input-row .primary-btn { margin: 10px 0 0; padding: 14px; }
  .kitchen-orders { grid-template-columns: repeat(2, 1fr); }
  .admin-heading { align-items: flex-start; }
  .admin-heading .ghost-link { margin-left: 12px; white-space: nowrap; }
  .admin-pin-grid { grid-template-columns: 1fr; }
  .admin-pin-fields { grid-template-columns: 1fr; }
}

@media (display-mode: fullscreen) {
  .topbar .view-switcher { display: none; }
  .tv-view { padding-top: 28px; }
}

/* TV layout: reserve pixels for pickup numbers, then fit every row inside its card. */
@media (min-width: 721px) {
  body[data-view="tv"] {
    --tv-header-height: 56px;
    overflow: hidden;
  }
  body[data-view="tv"] .topbar {
    height: var(--tv-header-height);
    padding: 0 clamp(14px, 2.5vw, 36px);
    gap: 12px;
  }
  body[data-view="tv"] .brand > span { margin-left: 8px; }
  body[data-view="tv"] .brand-logo { width: 70px; height: 42px; }
  body[data-view="tv"] .brand strong { font-size: 14px; }
  body[data-view="tv"] .brand small { display: none; }
  body[data-view="tv"] .settings-link { width: 34px; height: 34px; border-radius: 10px; }
  body[data-view="tv"] .settings-icon { width: 18px; height: 18px; }
  body[data-view="tv"] .clock { min-width: 70px; font-size: 13px; font-weight: 700; }
  body[data-view="tv"] main {
    height: calc(100vh - var(--tv-header-height));
    overflow: hidden;
  }
  body[data-view="tv"] .tv-view {
    width: min(3600px, 100%);
    height: 100%;
    min-height: 0;
    display: flex;
    flex-direction: column;
    padding: 12px clamp(14px, 2.5vw, 36px);
  }
  body[data-view="tv"] .hero-row {
    min-height: 30px;
    flex: 0 0 auto;
    align-items: center;
    margin-bottom: 10px;
  }
  body[data-view="tv"] .hero-row > div:first-child,
  body[data-view="tv"] .stall-head > div { min-width: 0; }
  body[data-view="tv"] .eyebrow { display: none; }
  body[data-view="tv"] h1 {
    font-size: clamp(20px, 2vw, 28px);
    line-height: 1.15;
    letter-spacing: -.02em;
  }
  body[data-view="tv"] .live-pill { padding: 5px 8px; font-size: 10px; }
  body[data-view="tv"] .live-pill span { width: 6px; height: 6px; margin-right: 3px; }
  body[data-view="tv"] .tv-grid {
    min-height: 0;
    flex: 1 1 auto;
    gap: 12px;
    grid-template-rows: repeat(2, minmax(0, 1fr));
  }
  body[data-view="tv"] .stall-card {
    min-height: 0;
    display: flex;
    flex-direction: column;
    padding: 12px;
    border-radius: 16px;
    border: 2px solid #cbd5e1;
    box-shadow: 0 12px 32px rgba(15, 23, 42, .13);
  }
  body[data-view="tv"] .stall-card::before { width: 5px; }
  body[data-view="tv"] .stall-head {
    position: relative;
    display: block;
    flex: 0 0 auto;
    text-align: center;
  }
  body[data-view="tv"] .stall-id { display: none; }
  body[data-view="tv"] .stall-head h2 {
    margin: 0;
    font-size: clamp(32px, 3vw, 64px);
    font-weight: 900;
    line-height: 1.12;
    overflow-wrap: anywhere;
    word-break: break-word;
  }
  body[data-view="tv"] .status-dot {
    position: absolute;
    top: 0;
    right: 0;
    padding: 4px 7px;
    font-size: 10px;
  }
  body[data-view="tv"] .number-list {
    --number-columns: 4;
    --number-rows: 9;
    --number-gap: 8px;
    min-height: 0;
    flex: 1 1 auto;
    margin-top: 8px;
    overflow: hidden;
  }
  body[data-view="tv"] .number-list.has-orders {
    display: grid;
    grid-template-columns: repeat(var(--number-columns, 1), minmax(0, 1fr));
    grid-template-rows: repeat(var(--number-rows, 1), minmax(0, 1fr));
    grid-auto-flow: row;
    gap: var(--number-gap, 8px);
  }
  body[data-view="tv"] .pickup-number {
    min-width: 0;
    min-height: 0;
    width: 100%;
    height: 100%;
    display: grid;
    place-items: center;
    padding: 4px 6px;
    border-radius: 10px;
    overflow: hidden;
    white-space: nowrap;
    font-size: var(--number-font, 32px);
    line-height: 1;
    letter-spacing: 0;
    background: transparent;
    border: 0;
    border-radius: 0;
    color: var(--ink);
  }
  body[data-view="tv"] .empty-state {
    align-self: center;
    padding: 8px;
  }
  body[data-view="tv"] .empty-state strong { margin-bottom: 3px; font-size: 14px; }
  body[data-view="tv"] .empty-state span { font-size: 11px; }
  body[data-view="tv"] .tv-note { display: none; }
}

@supports (height: 100dvh) {
  @media (min-width: 721px) {
    body[data-view="tv"] main { height: calc(100dvh - var(--tv-header-height)); }
  }
}

@media (min-width: 1500px) {
  body[data-view="tv"] .tv-grid { grid-template-rows: minmax(0, 1fr); }
  body[data-view="tv"] .number-list {
    --number-columns: 2;
    --number-rows: 9;
  }
}

@media (min-width: 721px) and (max-height: 520px) {
  body[data-view="tv"] { --tv-header-height: 48px; }
  body[data-view="tv"] .brand-logo { width: 58px; height: 34px; }
  body[data-view="tv"] .brand strong { font-size: 13px; }
  body[data-view="tv"] .tv-view { padding-top: 8px; padding-bottom: 8px; }
  body[data-view="tv"] .hero-row { min-height: 24px; margin-bottom: 6px; }
  body[data-view="tv"] h1 { font-size: 20px; }
  body[data-view="tv"] .tv-grid { gap: 8px; }
  body[data-view="tv"] .stall-card { padding: 9px; }
  body[data-view="tv"] .stall-id { display: none; }
  body[data-view="tv"] .number-list { margin-top: 6px; }
}
