:root {
  --bg: #17121f;
  --bg-2: #221833;
  --surface: #2a2038;
  --surface-2: #342848;
  --text: #f7f2ff;
  --muted: #b6a8cc;
  --line: rgba(255, 255, 255, 0.09);
  --accent: #ff7a45;
  --accent-2: #ffd166;
  --success: #3ddc97;
  --danger: #ff5d73;
  --gold-1: #ffe27a;
  --gold-2: #f59f00;
  --radius: 22px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: -apple-system, "SF Pro Rounded", "Rubik", "Assistant", "Segoe UI", Arial, sans-serif;
  background:
    radial-gradient(1100px 600px at 85% -10%, rgba(255, 122, 69, 0.22), transparent 60%),
    radial-gradient(900px 700px at 10% 110%, rgba(157, 78, 221, 0.25), transparent 60%),
    linear-gradient(160deg, var(--bg), var(--bg-2));
  background-attachment: fixed;
  color: var(--text);
  overflow: hidden;
}

.svg-library { position: absolute; width: 0; height: 0; overflow: hidden; }
svg { width: 1.15em; height: 1.15em; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }

.app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: calc(var(--safe-top) + 12px) 16px calc(96px + var(--safe-bottom));
  max-width: 560px;
  margin: 0 auto;
  overflow: hidden;
}

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 6px; flex: 0 0 auto; }
.topbar-start, .topbar-end, .topbar-history { display: flex; align-items: center; gap: 8px; }
.brand { font-size: 1.25rem; font-weight: 800; letter-spacing: -0.02em; white-space: nowrap; }
.brand-dot { color: var(--accent); }

.icon-button {
  position: relative;
  width: 46px; height: 46px;
  display: grid; place-items: center;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  font-size: 1.15rem;
  cursor: pointer;
  transition: transform 0.15s ease, background 0.2s ease;
}
.icon-button:active { transform: scale(0.92); }
.icon-button.accent { background: linear-gradient(140deg, rgba(255, 122, 69, 0.25), rgba(255, 209, 102, 0.14)); border-color: rgba(255, 160, 90, 0.35); }
.icon-button.small { width: 40px; height: 40px; border-radius: 13px; font-size: 1rem; }
.icon-button:disabled { opacity: 0.35; }
.icon-button .dot { position: absolute; top: 8px; right: 8px; width: 9px; height: 9px; border-radius: 50%; background: var(--accent-2); box-shadow: 0 0 8px var(--accent-2); }

.status-line {
  margin: 12px 4px 4px;
  color: var(--muted);
  font-size: 0.95rem;
  text-align: center;
  min-height: 1.4em;
  flex: 0 0 auto;
  transition: opacity 0.25s ease;
}

.stage {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-bottom: 6px;
}

.drawn { width: 100%; display: flex; flex-direction: column; align-items: center; gap: 14px; padding-bottom: 86px; }
.card-slot { width: min(84vw, 350px); perspective: 1200px; }

.card {
  position: relative;
  border-radius: 26px;
  background: linear-gradient(165deg, var(--g1, #ff7a45), var(--g2, #e63946));
  color: #fff;
  overflow: hidden;
  box-shadow: 0 26px 60px rgba(0, 0, 0, 0.45);
  transform-style: preserve-3d;
}
.card-slot.dealing .card { animation: card-in 0.7s cubic-bezier(0.22, 1.3, 0.36, 1) both; }

@keyframes card-in {
  0% { transform: rotateY(120deg) scale(0.55) translateY(60px); opacity: 0; filter: blur(6px); }
  60% { transform: rotateY(-14deg) scale(1.03) translateY(-6px); opacity: 1; filter: blur(0); }
  100% { transform: rotateY(0deg) scale(1) translateY(0); opacity: 1; }
}

.card-glow {
  position: absolute; inset: -40%;
  background: radial-gradient(circle at 50% 35%, rgba(255, 255, 255, 0.45), transparent 55%);
  opacity: 0.35;
  pointer-events: none;
}

.card-face { position: relative; padding: 22px 22px 16px; text-align: center; }
.tile {
  width: 90px; height: 90px;
  margin: 4px auto 10px;
  border-radius: 26px;
  display: grid; place-items: center;
  font-size: 2.9rem;
  background: rgba(255, 255, 255, 0.22);
  border: 1px solid rgba(255, 255, 255, 0.35);
  box-shadow: inset 0 -8px 18px rgba(0, 0, 0, 0.15), 0 10px 24px rgba(0, 0, 0, 0.22);
}
.card-slot.dealing .tile { animation: tile-pop 0.55s cubic-bezier(0.34, 1.7, 0.5, 1) 0.35s both; }
@keyframes tile-pop { 0% { transform: scale(0.4) rotate(-18deg); } 100% { transform: scale(1) rotate(0); } }

.card-category { margin: 0; font-size: 0.82rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.14em; opacity: 0.85; }
.card-name { margin: 4px 0 6px; font-size: 2rem; font-weight: 900; letter-spacing: -0.02em; text-shadow: 0 3px 14px rgba(0, 0, 0, 0.3); }
.card-meta { display: flex; justify-content: center; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.meta-chip {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 6px 11px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid rgba(255, 255, 255, 0.25);
  font-size: 0.85rem; font-weight: 700;
}
.meta-chip svg { width: 1em; height: 1em; }

.tier-badge {
  position: absolute; top: 14px; inset-inline-start: 14px;
  display: inline-flex; align-items: center; gap: 5px;
  padding: 6px 12px;
  border-radius: 999px;
  font-weight: 900; font-size: 0.85rem;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);
  z-index: 3;
}
.tier-badge svg { width: 1em; height: 1em; }
.tier-bronze { background: linear-gradient(120deg, #f0b27a, #c96f2f); color: #3a1e0a; }
.tier-silver { background: linear-gradient(120deg, #f1f3f5, #adb5bd); color: #212529; }
.tier-gold { background: linear-gradient(120deg, var(--gold-1), var(--gold-2)); color: #4a2c00; }

.card.gold-tier { background: linear-gradient(165deg, #ffd76a, #ff9f1c 55%, #e8590c); color: #3c2200; }
.card.gold-tier .meta-chip { background: rgba(255, 255, 255, 0.35); border-color: rgba(255, 255, 255, 0.6); color: #3c2200; }
.card.gold-tier .card-face { overflow: hidden; }
.card.gold-tier .card-face::after {
  content: "";
  position: absolute; inset: 0;
  background: linear-gradient(115deg, transparent 30%, rgba(255, 255, 255, 0.6) 45%, transparent 60%);
  background-size: 250% 100%;
  animation: shimmer 2.4s linear infinite;
  pointer-events: none;
}
@keyframes shimmer { 0% { background-position: 180% 0; } 100% { background-position: -80% 0; } }

.card-body {
  position: relative;
  background: rgba(18, 12, 26, 0.92);
  color: var(--text);
  padding: 18px 16px 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.12);
}

.thinking { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 0.95rem; min-height: 54px; }
.thinking-chef { display: grid; place-items: center; width: 38px; height: 38px; border-radius: 14px; background: var(--surface-2); color: var(--accent-2); animation: bob 1s ease-in-out infinite; }
@keyframes bob { 0%, 100% { transform: translateY(0) rotate(-4deg); } 50% { transform: translateY(-5px) rotate(5deg); } }

.ingredients { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 7px; }
.ing-row {
  display: flex; align-items: center; gap: 8px;
  min-height: 50px;
  padding: 5px 6px 5px 8px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--line);
  overflow: hidden;
}
.ing-name { flex: 1; font-size: 0.99rem; line-height: 1.4; }
.ing-actions { display: flex; gap: 6px; }
.ing-button {
  width: 42px; height: 42px;
  display: grid; place-items: center;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.07);
  color: var(--text);
  font-size: 1.05rem;
  cursor: pointer;
  transition: transform 0.12s ease, background 0.2s ease;
}
.ing-button:active { transform: scale(0.9); }
.ing-button.swap { color: var(--accent-2); }
.ing-button.remove { color: var(--danger); }
.dish.streaming .ing-actions { display: none; }
.ing-row.busy .ing-name { opacity: 0.35; }
.ing-row.busy .ing-actions { visibility: hidden; }
.ing-row.busy::after {
  content: "";
  position: absolute;
  inset-inline-end: 16px;
  top: 50%;
  width: 16px; height: 16px;
  translate: 0 -50%;
  border: 2px solid rgba(255, 209, 102, 0.25);
  border-top-color: var(--accent-2);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
.ing-row { position: relative; }
@keyframes spin { to { transform: rotate(360deg); } }
.ing-row.removing { animation: row-out 0.2s ease forwards; }
@keyframes row-out { to { opacity: 0; transform: translateX(-30px) scale(0.94); max-height: 0; margin-bottom: -7px; padding-top: 0; padding-bottom: 0; } }
.ing-row.swap-flash { animation: row-flash 0.7s ease; }
@keyframes row-flash {
  0% { box-shadow: inset 0 0 0 2px rgba(61, 220, 151, 0.9); background: rgba(61, 220, 151, 0.18); }
  100% { box-shadow: inset 0 0 0 0 rgba(61, 220, 151, 0); background: rgba(255, 255, 255, 0.05); }
}

.dish-hint { margin: 12px 0 0; color: var(--muted); font-size: 0.83rem; text-align: center; line-height: 1.45; }

.reveal { animation: reveal 0.42s cubic-bezier(0.2, 0.8, 0.3, 1) both; }
@keyframes reveal {
  0% { opacity: 0; transform: translateY(8px); filter: blur(5px); }
  100% { opacity: 1; transform: translateY(0); filter: blur(0); }
}

.note-pill {
  display: inline-flex; align-items: center; gap: 8px;
  max-width: 90%;
  padding: 9px 15px;
  border-radius: 999px;
  border: 1px solid rgba(255, 209, 102, 0.45);
  background: rgba(255, 209, 102, 0.14);
  color: #ffe9b8;
  font-size: 0.9rem; font-weight: 700;
  cursor: pointer;
  flex: 0 0 auto;
}
.note-pill span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.actions {
  position: fixed;
  bottom: calc(96px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  z-index: 25;
  width: min(92vw, 380px);
  display: grid;
  grid-template-columns: 1.4fr 1fr;
  gap: 10px;
  padding: 10px;
  border-radius: 24px;
  background: rgba(23, 18, 31, 0.92);
  border: 1px solid var(--line);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.45);
}
.action {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  min-height: 52px;
  padding: 12px 10px;
  border-radius: 18px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font-size: 0.95rem; font-weight: 800;
  font-family: inherit;
  cursor: pointer;
  transition: transform 0.14s ease, filter 0.2s ease;
}
.action:active { transform: scale(0.96); }
.action:disabled { opacity: 0.55; }
.action.primary { background: linear-gradient(140deg, var(--accent), #ff5d73); border: none; color: #fff; box-shadow: 0 10px 26px rgba(255, 93, 115, 0.35); }
.action.wide { width: 100%; }

.edge-arrow {
  position: fixed;
  top: 50%;
  transform: translateY(-50%);
  z-index: 26;
  width: 46px; height: 46px;
  display: grid; place-items: center;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.16);
  background: rgba(42, 32, 56, 0.82);
  color: var(--text);
  font-size: 1.3rem;
  cursor: pointer;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.4);
  transition: transform 0.14s ease, opacity 0.2s ease;
}
.edge-arrow:active { transform: translateY(-50%) scale(0.9); }
.edge-arrow.prev { right: 7px; }
.edge-arrow.next { left: 7px; }

.deck-zone {
  position: fixed;
  left: 50%;
  bottom: calc(8px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 30;
  transition: bottom 0.45s cubic-bezier(0.3, 0.9, 0.3, 1);
}
body[data-mode="idle"] .deck-zone { bottom: calc(50dvh - 54px); }

.deck {
  position: relative;
  display: block;
  width: 172px;
  height: 100px;
  padding: 0;
  border: none;
  background: none;
  color: var(--text);
  font-family: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.deck:active { transform: scale(0.96); }

.deck-pile { position: absolute; inset: 0; }
.deck-pile i {
  position: absolute;
  left: 50%; top: 50%;
  width: 136px; height: 82px;
  margin: -41px 0 0 -68px;
  border-radius: 15px;
  background: linear-gradient(150deg, var(--p1, #ff7a45), var(--p2, #e63946));
  border: 1px solid rgba(255, 255, 255, 0.35);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.45);
}
.deck-pile i:nth-child(1) { transform: rotate(-6deg) translate(-8px, 5px); opacity: 0.7; }
.deck-pile i:nth-child(2) { transform: rotate(4deg) translate(7px, -3px); opacity: 0.85; }
.deck-pile i:nth-child(3) { transform: rotate(-2.5deg) translate(-3px, -6px); }

.deck-face {
  position: absolute;
  left: 50%; top: 50%;
  width: 136px; height: 82px;
  margin: -41px 0 0 -68px;
  border-radius: 15px;
  background: linear-gradient(150deg, var(--p1, #ff7a45), var(--p2, #e63946));
  border: 1px solid rgba(255, 255, 255, 0.55);
  box-shadow: 0 12px 26px rgba(0, 0, 0, 0.5);
  transform: rotate(1.8deg);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
}
.deck-emoji { font-size: 1.55rem; line-height: 1; }
.deck-text { display: flex; flex-direction: column; align-items: flex-start; gap: 1px; }
.deck-name { font-size: 1.05rem; font-weight: 900; letter-spacing: -0.01em; white-space: nowrap; }
.deck-count { font-size: 0.72rem; font-weight: 700; opacity: 0.9; white-space: nowrap; }

.deck-zone.switching .deck-face { animation: label-swap 0.34s ease both; }
@keyframes label-swap {
  0% { opacity: 1; transform: rotate(1.8deg) translateX(0); }
  45% { opacity: 0; transform: rotate(1.8deg) translateX(-14px); }
  55% { opacity: 0; transform: rotate(1.8deg) translateX(14px); }
  100% { opacity: 1; transform: rotate(1.8deg) translateX(0); }
}

.deck-zone.dealing .deck-pile i { animation: pile-jump 0.55s cubic-bezier(0.3, 1.1, 0.4, 1) both; }
.deck-zone.dealing .deck-pile i:nth-child(2) { animation-delay: 0.05s; }
.deck-zone.dealing .deck-pile i:nth-child(3) { animation-delay: 0.1s; }
.deck-zone.dealing .deck-face { animation: pile-jump 0.55s cubic-bezier(0.3, 1.1, 0.4, 1) 0.15s both; }
@keyframes pile-jump {
  0% { translate: 0 0; }
  42% { translate: 0 -24px; }
  100% { translate: 0 0; }
}

.deck.nudging { animation: deck-nudge 0.65s ease; }
@keyframes deck-nudge {
  0%, 100% { transform: rotate(0) translate(0, 0); }
  20% { transform: rotate(-3deg) translate(-5px, -6px); }
  45% { transform: rotate(3deg) translate(5px, -4px); }
  70% { transform: rotate(-1.5deg) translate(-2px, -1px); }
}

.fly-card {
  position: fixed;
  z-index: 34;
  border-radius: 16px;
  background: linear-gradient(150deg, var(--g1, #ff7a45), var(--g2, #e63946));
  border: 1px solid rgba(255, 255, 255, 0.45);
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.5);
  display: grid;
  place-items: center;
  font-size: 1.9rem;
  pointer-events: none;
  transform-origin: center;
}

dialog.sheet {
  border: none;
  padding: 0;
  width: 100%;
  max-width: 560px;
  margin: auto auto 0;
  background: var(--surface);
  color: var(--text);
  border-radius: 26px 26px 0 0;
  box-shadow: 0 -20px 60px rgba(0, 0, 0, 0.5);
  max-height: 88dvh;
}
dialog.sheet[open] { animation: sheet-in 0.32s cubic-bezier(0.2, 0.9, 0.3, 1); }
dialog.sheet::backdrop { background: rgba(8, 5, 14, 0.62); backdrop-filter: blur(3px); }
@keyframes sheet-in { from { transform: translateY(16%); opacity: 0.4; } to { transform: translateY(0); opacity: 1; } }

.sheet-body { padding: 20px 20px calc(24px + var(--safe-bottom)); display: flex; flex-direction: column; gap: 12px; overflow-y: auto; }
.sheet-heading { display: flex; align-items: center; justify-content: space-between; }
.sheet-heading h2 { margin: 0; font-size: 1.35rem; font-weight: 900; }
.sheet-label { margin: 6px 0 0; font-size: 0.8rem; letter-spacing: 0.14em; text-transform: uppercase; color: var(--accent-2); font-weight: 800; }
.sheet-hint { margin: 0; color: var(--muted); font-size: 0.9rem; line-height: 1.5; }
.sheet-actions { display: grid; grid-template-columns: 1fr 1.4fr; gap: 10px; margin-top: 4px; }

.chip-grid { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  padding: 10px 14px;
  min-height: 44px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  font-family: inherit;
  font-size: 0.95rem; font-weight: 700;
  cursor: pointer;
  transition: transform 0.12s ease;
}
.chip:active { transform: scale(0.95); }
.chip.selected { background: linear-gradient(140deg, rgba(255, 122, 69, 0.9), rgba(255, 93, 115, 0.9)); border-color: transparent; color: #fff; }

textarea {
  width: 100%;
  resize: vertical;
  padding: 14px;
  border-radius: 16px;
  border: 1px solid var(--line);
  background: rgba(0, 0, 0, 0.28);
  color: var(--text);
  font-family: inherit;
  font-size: 1rem;
  line-height: 1.6;
}
textarea:focus { outline: 2px solid rgba(255, 209, 102, 0.5); }

.toggle-row, .model-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 4px; font-weight: 700; }
.toggle-row input { width: 52px; height: 30px; accent-color: var(--accent); }
.model-status { color: var(--muted); font-size: 0.9rem; }
.model-status.ok { color: var(--success); }
.model-status.bad { color: var(--danger); }

.form-error { margin: 0; color: var(--danger); font-size: 0.9rem; min-height: 1.2em; }

.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat-tile { padding: 14px 8px; border-radius: 18px; background: rgba(255, 255, 255, 0.05); border: 1px solid var(--line); text-align: center; }
.stat-value { font-size: 1.5rem; font-weight: 900; color: var(--accent-2); }
.stat-label { font-size: 0.78rem; color: var(--muted); margin-top: 2px; }
.recent-list { display: flex; flex-direction: column; gap: 8px; }
.recent-row { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 14px; background: rgba(255, 255, 255, 0.04); border: 1px solid var(--line); font-size: 0.95rem; }
.recent-row .when { margin-inline-start: auto; color: var(--muted); font-size: 0.82rem; }

#toast {
  position: fixed;
  bottom: calc(174px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  z-index: 40;
  background: #fff;
  color: #1b1226;
  padding: 12px 18px;
  border-radius: 999px;
  font-weight: 800;
  font-size: 0.95rem;
  box-shadow: 0 14px 34px rgba(0, 0, 0, 0.4);
  animation: toast-in 0.3s ease both;
  max-width: 90vw;
  text-align: center;
}
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 14px); } to { opacity: 1; transform: translate(-50%, 0); } }

.burst-layer { position: fixed; inset: 0; pointer-events: none; z-index: 35; overflow: hidden; }
.burst-particle {
  position: absolute;
  width: 10px; height: 10px;
  border-radius: 3px;
  opacity: 0;
}

.empty { text-align: center; color: var(--muted); padding: 40px 20px; }

@media (min-width: 600px) {
  .actions { width: min(92vw, 520px); }
  .card-slot { width: min(70vw, 420px); }
  .action { font-size: 0.95rem; }
  .edge-arrow.prev { right: calc(50vw - 300px); }
  .edge-arrow.next { left: calc(50vw - 300px); }
}

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