/* Token Table — styles. Site palette for the chrome; Magic's five colours for the cards. */

:root {
  --paper: #f3ede1;
  --paper-raised: #faf6ee;
  --ink: #1a1814;
  --ink-soft: #c9c1b1;
  --ink-soft-text: #5e564a;
  --accent: #a8201a;
  --display: 'Cormorant Garamond', 'Times New Roman', serif;
  --body: 'Source Serif 4', Georgia, serif;

  /* Magic colours (card frames) */
  --mtg-w: #f4ecd2; --mtg-w-ink: #3b3320;
  --mtg-u: #b9d3ea; --mtg-u-ink: #10314f;
  --mtg-b: #5d5761; --mtg-b-ink: #f2ecf0;
  --mtg-r: #e59d86; --mtg-r-ink: #4a140c;
  --mtg-g: #a9cfa0; --mtg-g-ink: #143314;
  --mtg-c: #d9d6cf; --mtg-c-ink: #2a2924;
  --mtg-m: #e5c86b; --mtg-m-ink: #3d2f05;
  --mtg-a: #cbd0d6; --mtg-a-ink: #1f2429;   /* artifact, non-creature */

  --tile-min: 158px;
  --hit: 44px;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(26,24,20,.15), 0 4px 12px rgba(26,24,20,.08);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { touch-action: manipulation; -webkit-text-size-adjust: 100%; }
html, body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--body);
  font-size: 16px;
  line-height: 1.35;
}
body { height: 100vh; height: 100dvh; display: flex; flex-direction: column; overflow: hidden; }

button { font-family: var(--body); font-size: 15px; color: var(--ink); cursor: pointer; }
button, .card, .chip, .tile { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
img { -webkit-user-drag: none; }

.btn {
  min-height: var(--hit); min-width: var(--hit);
  white-space: nowrap;
  padding: 0 14px;
  border: 1px solid var(--ink);
  border-radius: 8px;
  background: var(--paper-raised);
  line-height: 1;
}
.btn:active { background: var(--ink-soft); }
.btn[disabled] { opacity: .4; cursor: default; }
.btn-primary { background: var(--ink); color: var(--paper); }
.btn-primary:active { background: #000; }
.btn-danger { border-color: var(--accent); color: var(--accent); }
.btn-icon { padding: 0; width: var(--hit); font-size: 22px; }
.btn-wide { width: 100%; }

/* ---------------- header ---------------- */

#top {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: max(8px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  border-bottom: 2px solid var(--ink);
  background: var(--paper-raised);
}
.top-left { display: flex; flex-direction: column; min-width: 0; }
#back-link { font-size: 12px; color: var(--ink-soft-text); text-decoration: none; white-space: nowrap; }
#back-link:hover { color: var(--accent); }
#top h1 { margin: 0; font-family: var(--display); font-weight: 600; font-size: 24px; line-height: 1.1; white-space: nowrap; }
.top-right { display: flex; align-items: center; gap: 8px; }
.turn { font-family: var(--display); font-size: 18px; font-weight: 600; white-space: nowrap; }

/* ---------------- layout ---------------- */

#app { flex: 1; display: flex; flex-direction: column; min-height: 0; }
#board { order: 0; } #tray { order: 1; } #palette { order: 2; }

#board { flex: 1; min-height: 0; padding: 12px max(12px, env(safe-area-inset-right)) 12px max(12px, env(safe-area-inset-left)); overflow-y: auto; -webkit-overflow-scrolling: touch; }
.empty { color: var(--ink-soft-text); font-style: italic; text-align: center; margin: 32px 12px; }

.stacks {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(var(--tile-min), 1fr));
  gap: 12px;
  align-items: start;
}

/* palette */
#palette { border-top: 1px solid var(--ink-soft); background: var(--paper-raised); padding: 8px 0 max(8px, env(safe-area-inset-bottom)); }
.palette-row {
  display: flex; gap: 8px; overflow-x: auto; -webkit-overflow-scrolling: touch;
  padding: 2px max(12px, env(safe-area-inset-right)) 6px max(12px, env(safe-area-inset-left));
  scrollbar-width: none;
}
.palette-row::-webkit-scrollbar { display: none; }
.chips { display: contents; }
.chip {
  flex: 0 0 auto;
  min-height: var(--hit);
  padding: 0 14px;
  border: 1px solid var(--ink-soft);
  border-radius: 999px;
  background: var(--paper);
  font-family: var(--display); font-weight: 600; font-size: 17px;
  white-space: nowrap;
}
.chip:active { background: var(--ink-soft); }
.chip .pt { font-family: var(--body); font-size: 12px; color: var(--ink-soft-text); margin-left: 6px; }
.chip-action { border-style: dashed; font-family: var(--body); font-weight: 400; font-size: 15px; }
.chip.c-w { background: var(--mtg-w); } .chip.c-u { background: var(--mtg-u); } .chip.c-b { background: var(--mtg-b); color: var(--mtg-b-ink); }
.chip.c-r { background: var(--mtg-r); } .chip.c-g { background: var(--mtg-g); } .chip.c-m { background: var(--mtg-m); }
.chip.c-a, .chip.c-c { background: var(--mtg-a); }
.chip.c-b .pt { color: #d9d2dd; }

/* tray */
#tray {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 8px max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  border-top: 2px solid var(--ink);
  background: var(--paper-raised);
}
.tray-life { display: flex; align-items: center; gap: 6px; }
.life { font-family: var(--display); font-size: 28px; font-weight: 700; min-width: 3ch; text-align: center; line-height: 1; }
.life small { display: block; font-family: var(--body); font-size: 10px; font-weight: 400; color: var(--ink-soft-text); text-transform: uppercase; letter-spacing: .08em; }
.tray-mana { display: flex; gap: 6px; }
.pip {
  width: var(--hit); height: var(--hit);
  border-radius: 50%; border: 2px solid var(--ink);
  font-family: var(--display); font-weight: 700; font-size: 18px;
  display: grid; place-items: center; padding: 0;
  position: relative;
}
.pip.zero { opacity: .45; }
.pip-w { background: var(--mtg-w); } .pip-u { background: var(--mtg-u); } .pip-b { background: var(--mtg-b); color: var(--mtg-b-ink); }
.pip-r { background: var(--mtg-r); } .pip-g { background: var(--mtg-g); } .pip-c { background: var(--mtg-c); }
.pip .n { position: absolute; top: -6px; right: -6px; min-width: 18px; height: 18px; border-radius: 9px; background: var(--ink); color: var(--paper); font-family: var(--body); font-size: 12px; line-height: 18px; padding: 0 4px; }
.pip.zero .n { display: none; }
.summary { margin: 0; flex-basis: 100%; font-size: 12px; color: var(--ink-soft-text); }

/* ---------------- tiles ---------------- */

.tile { display: flex; flex-direction: column; gap: 6px; }
.slot { width: 100%; aspect-ratio: 1 / 1; display: grid; place-items: center; }
.card {
  width: 71.4%;  /* 1/1.4: the rotated card fits inside the square slot */
  aspect-ratio: 5 / 7;
  border-radius: var(--radius);
  border: 2px solid rgba(26,24,20,.45);
  box-shadow: var(--shadow);
  position: relative;
  overflow: hidden;
  display: flex; flex-direction: column;
  transition: transform .15s ease;
  will-change: transform;
  background: var(--mtg-c); color: var(--mtg-c-ink);
}
@media (prefers-reduced-motion: reduce) { .card { transition: none; } }
.tile.tapped .card { transform: rotate(90deg); }
.card.c-w { background: var(--mtg-w); color: var(--mtg-w-ink); }
.card.c-u { background: var(--mtg-u); color: var(--mtg-u-ink); }
.card.c-b { background: var(--mtg-b); color: var(--mtg-b-ink); }
.card.c-r { background: var(--mtg-r); color: var(--mtg-r-ink); }
.card.c-g { background: var(--mtg-g); color: var(--mtg-g-ink); }
.card.c-m { background: var(--mtg-m); color: var(--mtg-m-ink); }
.card.c-a { background: var(--mtg-a); color: var(--mtg-a-ink); }
.card.c-c { background: var(--mtg-c); color: var(--mtg-c-ink); }

.card .art { flex: 0 0 52%; background: rgba(26,24,20,.08) center/cover no-repeat; border-bottom: 1px solid rgba(26,24,20,.25); }
.card .art img { width: 100%; height: 100%; object-fit: cover; display: block; }
.card.no-art .art { flex-basis: 30%; background-image: repeating-linear-gradient(135deg, rgba(26,24,20,.05) 0 6px, transparent 6px 12px); }
.card .body { flex: 1; padding: 6px 8px 6px; display: flex; flex-direction: column; min-height: 0; }
.card.has-pt .body { padding-right: 50px; }
.card .name { font-family: var(--display); font-weight: 700; font-size: 18px; line-height: 1.05; }
.card .type { font-size: 10px; opacity: .8; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .text { font-size: 10px; line-height: 1.2; margin-top: 4px; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; }
.card .pt {
  position: absolute; right: 6px; bottom: 6px;
  font-family: var(--display); font-weight: 700; font-size: 22px; line-height: 1;
  padding: 3px 7px; border-radius: 6px; background: rgba(250,246,238,.85); color: var(--ink);
  border: 1px solid rgba(26,24,20,.35);
}
.card .pt.dead { background: var(--accent); color: #fff; border-color: var(--accent); }
.card .pt.boosted { border-color: #a8790f; box-shadow: 0 0 0 2px rgba(168,121,15,.45) inset; }
.card .count {
  position: absolute; left: 6px; top: 6px;
  font-family: var(--display); font-weight: 700; font-size: 20px; line-height: 1;
  padding: 4px 8px; border-radius: 999px; background: var(--ink); color: var(--paper);
  border: 2px solid rgba(250,246,238,.7);
}
.card .sick {
  position: absolute; right: 6px; top: 6px;
  font-size: 11px; font-style: italic; padding: 2px 6px; border-radius: 999px;
  background: rgba(250,246,238,.9); color: var(--ink-soft-text); border: 1px solid rgba(26,24,20,.3);
}
.card .counters { position: absolute; left: 6px; bottom: 6px; display: flex; gap: 4px; flex-wrap: wrap; max-width: 60%; }
.card .counters span { font-size: 11px; font-weight: 600; padding: 2px 6px; border-radius: 999px; background: rgba(250,246,238,.9); color: var(--ink); border: 1px solid rgba(26,24,20,.35); }
.card .credit { position: absolute; left: 0; right: 0; top: calc(52% - 14px); font-size: 8px; padding: 1px 6px; background: rgba(26,24,20,.55); color: #fff; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.actions { display: grid; gap: 4px; }
.actions .r { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px; }
.actions .btn { min-width: 0; padding: 0 4px; font-size: 14px; font-weight: 600; }
.actions .btn.pm { font-size: 20px; }

/* ---------------- sheets ---------------- */

.sheet-backdrop { position: fixed; inset: 0; background: rgba(26,24,20,.45); z-index: 20; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21;
  max-height: 88dvh; display: flex; flex-direction: column;
  background: var(--paper); border-top: 2px solid var(--ink);
  border-radius: 16px 16px 0 0;
  padding-bottom: env(safe-area-inset-bottom);
  box-shadow: 0 -8px 32px rgba(26,24,20,.25);
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px 6px 16px; border-bottom: 1px solid var(--ink-soft); }
.sheet-head h2 { margin: 0; font-family: var(--display); font-size: 22px; font-weight: 600; }
.sheet-body { overflow-y: auto; padding: 12px 16px 16px; -webkit-overflow-scrolling: touch; }
.sheet-body p { margin: 0 0 10px; }
.menu { display: grid; gap: 8px; }
.menu .btn { justify-content: flex-start; text-align: left; display: flex; align-items: center; gap: 10px; padding: 0 14px; }
.menu .btn small { color: var(--ink-soft-text); margin-left: auto; font-size: 12px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 8px 0; }
.row .grow { flex: 1; }
.field { display: grid; gap: 4px; margin-bottom: 10px; }
.field label { font-size: 12px; color: var(--ink-soft-text); text-transform: uppercase; letter-spacing: .06em; }
.field input[type=text], .field input[type=number], .field textarea, .field select, .search input {
  font: inherit; font-size: 16px; padding: 10px 12px; min-height: var(--hit);
  border: 1px solid var(--ink); border-radius: 8px; background: var(--paper-raised); color: var(--ink); width: 100%;
}
.field textarea { min-height: 72px; resize: vertical; }
.colors { display: flex; gap: 6px; }
.colors label { display: grid; place-items: center; width: var(--hit); height: var(--hit); border-radius: 50%; border: 2px solid var(--ink); font-family: var(--display); font-weight: 700; font-size: 18px; cursor: pointer; }
.colors input { position: absolute; opacity: 0; width: 0; height: 0; }
.colors label.pip-b { color: var(--mtg-b-ink); }
.colors label.on { outline: 3px solid var(--accent); outline-offset: 2px; }
input[type=range] { width: 100%; height: var(--hit); }
.big { font-family: var(--display); font-size: 40px; font-weight: 700; text-align: center; line-height: 1; margin: 8px 0; }
.note { font-size: 13px; color: var(--ink-soft-text); }
.reminder { border-left: 4px solid var(--accent); padding: 8px 12px; background: var(--paper-raised); margin-bottom: 12px; }
.reminder b { display: block; font-family: var(--display); font-size: 20px; }
.picker { display: flex; gap: 8px; flex-wrap: wrap; }

.results { display: grid; gap: 6px; }
.results .btn { justify-content: flex-start; text-align: left; display: flex; gap: 10px; align-items: center; }
.results .btn .meta { color: var(--ink-soft-text); font-size: 12px; margin-left: auto; white-space: nowrap; }
.search { margin-bottom: 10px; }

.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.gallery button { padding: 0; border: 2px solid transparent; border-radius: 8px; background: none; overflow: hidden; display: block; text-align: left; }
.gallery button.on { border-color: var(--accent); }
.gallery img { width: 100%; aspect-ratio: 5 / 7; object-fit: cover; display: block; background: var(--ink-soft); border-radius: 6px; }
.gallery .label { font-size: 11px; padding: 3px 2px 0; color: var(--ink-soft-text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.gallery .blank { width: 100%; aspect-ratio: 5 / 7; border-radius: 6px; border: 1px dashed var(--ink); display: grid; place-items: center; font-size: 12px; }
.section-title { font-family: var(--display); font-size: 18px; font-weight: 600; margin: 14px 0 6px; }

.toast {
  position: fixed; left: 50%; bottom: calc(96px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--ink); color: var(--paper); padding: 10px 16px; border-radius: 999px; font-size: 14px; z-index: 30;
  max-width: calc(100% - 32px); text-align: center;
}

.about { font-size: 14px; }
.about p { margin: 0 0 8px; }

/* ---------------- anthems & effects ---------------- */

.effects-strip {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  padding: 8px max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  border-bottom: 2px solid var(--ink);
  background: #fff7e0;
}
.chip-anthem { background: #fbecb8; border-color: #a8790f; font-size: 14px; font-weight: 600; }
.chip-anthem:active { background: #f3dd8f; }

.anthem-row { display: flex; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--ink-soft); align-items: flex-start; }
.anthem-row:last-child { border-bottom: none; }
.anthem-row-body { flex: 1; min-width: 0; }
.anthem-row-name { font-family: var(--display); font-weight: 600; font-size: 16px; }
.anthem-row-name small { display: block; font-family: var(--body); font-weight: 400; font-size: 11px; color: var(--ink-soft-text); }

.toggle {
  flex: 0 0 auto; width: 52px; height: 30px; margin-top: 2px;
  border-radius: 15px; border: 2px solid var(--ink); background: var(--paper-raised); position: relative; padding: 0;
}
.toggle::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 22px; height: 22px; border-radius: 50%;
  background: var(--ink); transition: transform .15s ease;
}
.toggle.on { background: #a8790f; border-color: #a8790f; }
.toggle.on::after { transform: translateX(22px); background: #fff; }
@media (prefers-reduced-motion: reduce) { .toggle::after { transition: none; } }

/* ---------------- wide screens (iPad landscape, desktop) ---------------- */

@media (min-width: 760px) {
  :root { --tile-min: 176px; }
  #app { display: grid; grid-template-columns: 200px minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto; }
  #palette { grid-column: 1; grid-row: 1; order: 0; border-top: 0; border-right: 1px solid var(--ink-soft); overflow-y: auto; padding: 12px 0; }
  .palette-row { flex-direction: column; overflow: visible; padding: 0 12px; }
  .chip { width: 100%; text-align: left; display: flex; justify-content: space-between; align-items: center; }
  #board { grid-column: 2; grid-row: 1; order: 0; }
  #tray { grid-column: 1 / -1; grid-row: 2; order: 0; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .summary { flex-basis: auto; margin-left: auto; }
  .sheet { left: 50%; right: auto; bottom: auto; top: 50%; transform: translate(-50%, -50%); width: min(560px, 92vw); border-radius: 16px; border: 2px solid var(--ink); max-height: 86dvh; }
}
@media (min-width: 1100px) { :root { --tile-min: 190px; } }

/* narrow phones: keep the header on one line */
@media (max-width: 480px) {
  #top h1 { font-size: 20px; }
  .turn { display: none; }   /* the turn number is repeated in the summary line */
  #btn-next { padding: 0 10px; font-size: 14px; }
  #btn-undo { padding: 0 10px; font-size: 14px; }
}
