/* Card Drawing Demo — see specs.md §4 */

:root {
  --bg: #f4f1ea;
  --panel: #ffffff;
  --ink: #1f2328;
  --muted: #5f6670;
  --line: #d9d4c7;
  --accent: #1f6feb;
  --accent-ink: #ffffff;
  --accent-soft: #e3edfd;
  --danger: #c62828;
  --danger-soft: #fdecea;
  --table: #2f6b4f;
  --table-ink: #f2f7f4;
  --radius: 10px;
  --card-radius: 8px;
  --hand-card-w: 100px;
  color-scheme: light;
}

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

/* Keep focused/scrolled-to elements clear of the sticky action bar. */
html { -webkit-text-size-adjust: 100%; scroll-padding-bottom: 140px; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
}

#app {
  max-width: 1200px;
  margin: 0 auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

h2 { margin: 0; font-size: 1.1rem; }

button {
  font: inherit;
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfaf7;
  color: var(--ink);
  cursor: pointer;
}
button:hover:not(:disabled) { background: #f0ede4; }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button.primary:hover:not(:disabled) { background: #1a5fcc; }
button.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
button.danger:hover:not(:disabled) { background: #a92020; }
button.danger-quiet { color: var(--danger); }
button.danger-quiet:hover:not(:disabled) { background: var(--danger-soft); }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

input[type="text"], input[type="number"] {
  font: inherit;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  min-width: 0;
}
input[aria-invalid="true"] { border-color: var(--danger); box-shadow: 0 0 0 2px var(--danger-soft); }

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
}

.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.spacer { flex: 1; }
.total { color: var(--muted); }

.notice {
  margin: 0;
  padding: 8px 10px;
  border-radius: 6px;
  background: #fff6db;
  border: 1px solid #ecd999;
}

/* ---------- Card faces (shared) ---------- */

.face {
  --hue: 210;
  position: relative;
  width: 100%;
  aspect-ratio: 5 / 7;
  border-radius: var(--card-radius);
  overflow: hidden;
  background: hsl(var(--hue) 45% 80%);
  container-type: inline-size; /* placeholder initials scale with the card */
}
.face img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background:
    radial-gradient(circle at 30% 25%, hsl(var(--hue) 55% 88%), transparent 60%),
    hsl(var(--hue) 45% 74%);
  color: hsl(var(--hue) 45% 26%);
  font-weight: 700;
  font-size: 38cqi;
  letter-spacing: 0.02em;
  user-select: none;
}
.caption {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  padding: 3px 6px;
  background: hsl(var(--hue) 45% 30% / 0.88);
  color: #fff;
  font-size: 0.78rem;
  font-weight: 600;
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: left;
}

.thumb {
  flex: none;
  width: 50px;
  border-radius: 5px;
  --card-radius: 5px;
}
.thumb:empty { aspect-ratio: 5 / 7; background: #eee; }

/* ---------- Deck builder ---------- */

.builder { padding: 0; }
.builder-heading { margin: 0; font-size: inherit; }
.builder-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px 14px;
  border: none;
  border-radius: var(--radius);
  background: transparent;
  text-align: left;
}
.builder-bar:hover:not(:disabled) { background: #f7f5ef; }
.builder-title { font-size: 1.1rem; font-weight: 700; }
.builder-summary { flex: 1; color: var(--muted); font-size: 0.9rem; }
.chevron {
  width: 10px; height: 10px;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: rotate(45deg) translate(-2px, -2px);
  transition: transform 0.15s ease;
}
.builder.collapsed .chevron { transform: rotate(-45deg); }
.builder.collapsed .builder-body { display: none; }
.builder:not(.collapsed) .builder-bar { border-radius: var(--radius) var(--radius) 0 0; border-bottom: 1px solid var(--line); }

.builder-body { display: flex; flex-direction: column; gap: 12px; padding: 14px; }

.add-form {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 10px;
  padding: 12px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: #fcfbf8;
}
.add-image { display: flex; align-items: flex-end; gap: 8px; }
.add-image-buttons { display: flex; flex-direction: column; gap: 6px; }
.field { display: flex; flex-direction: column; gap: 3px; font-size: 0.85rem; color: var(--muted); }
.field input { color: var(--ink); font-size: 15px; }
.field-name { flex: 1 1 160px; }
.field-desc { flex: 2 1 220px; }
.field-qty input { width: 72px; }

.empty-hint { margin: 0; color: var(--muted); }

.def-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 420px;
  overflow-y: auto;
}
.def-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
}
.def-fields { display: flex; flex: 1 1 260px; gap: 6px; flex-wrap: wrap; min-width: 0; }
.def-name { flex: 1 1 120px; font-weight: 600; }
.def-desc { flex: 2 1 160px; }
.qty { display: flex; align-items: center; gap: 4px; }
.qty button { width: 32px; padding: 6px 0; }
.qty input { width: 56px; text-align: center; }
.def-buttons { display: flex; gap: 6px; flex-wrap: wrap; }

.builder-footer { padding-top: 4px; border-top: 1px solid var(--line); padding-top: 12px; }

/* ---------- Messages ---------- */

.messages { display: flex; flex-direction: column; gap: 6px; }
.messages:empty { display: none; }
.message {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid #b9d0f5;
  background: var(--accent-soft);
}
.message-error { border-color: #f0b5af; background: var(--danger-soft); }
.message-progress { border-color: var(--line); background: #fff; }
.message-close { border: none; background: transparent; font-size: 1.2rem; line-height: 1; padding: 2px 6px; }

/* ---------- Table: draw pile + discard ---------- */

.table {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  background: var(--table);
  border-color: #24563f;
  color: var(--table-ink);
}
.pile-group { display: flex; align-items: center; gap: 14px; }
.pile { width: 80px; position: relative; }
.pile.stacked::before {
  content: "";
  position: absolute;
  inset: 0;
  transform: translate(4px, 4px);
  border-radius: var(--card-radius);
  background: #163a2a;
}
.pile-info { display: flex; flex-direction: column; gap: 8px; }
.discard-group .pile-info { align-items: flex-end; }
.pile-count { font-weight: 700; font-size: 1.05rem; }
.pile-buttons { display: flex; gap: 8px; flex-wrap: wrap; }
.card-back, .pile-empty {
  position: relative;
  width: 100%;
  aspect-ratio: 5 / 7;
  border-radius: var(--card-radius);
  display: flex;
  align-items: center;
  justify-content: center;
}
.card-back {
  background:
    repeating-linear-gradient(45deg, #8c2f39 0 6px, #a4434d 6px 12px);
  border: 4px solid #f3efe6;
  box-shadow: 0 2px 6px rgb(0 0 0 / 0.35);
}
.card-back span {
  background: #f3efe6;
  color: #5a1d24;
  font-weight: 700;
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 0.85rem;
}
.pile-empty {
  border: 2px dashed rgb(255 255 255 / 0.45);
  color: rgb(255 255 255 / 0.75);
  font-size: 0.75rem;
  text-align: center;
  padding: 4px;
}
#discard-top .face { box-shadow: 0 2px 6px rgb(0 0 0 / 0.35); }
.table button:not(.primary) { background: #f3efe6; border-color: #f3efe6; }
.table button:not(.primary):hover:not(:disabled) { background: #fff; }

.card-list {
  flex-basis: 100%;
  list-style: none;
  margin: 0;
  padding: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  background: rgb(0 0 0 / 0.15);
  border-radius: 8px;
  max-height: 240px;
  overflow-y: auto;
}
.card-list li { display: flex; flex-direction: column; align-items: center; gap: 3px; width: 64px; font-size: 0.72rem; text-align: center; }
.card-list li .face { --card-radius: 5px; }
.card-list li > span { width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-list-title { flex-basis: 100%; margin: 0; font-size: 0.8rem; font-weight: 700; opacity: 0.85; }
.card-count {
  position: absolute;
  right: 3px; top: 3px;
  padding: 0 5px;
  border-radius: 999px;
  background: #f3efe6;
  color: #1f2328;
  font-weight: 700;
  font-size: 0.7rem;
}

/* ---------- Hands ---------- */

.hands {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}
.hand {
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  min-height: 220px;
  min-width: 0;
}
.hand:not(.active) { cursor: pointer; }
.hand:not(.active):hover { border-color: #b9c8e6; }
.hand.active { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.hand-head {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  border: none;
  border-bottom: 1px solid var(--line);
  border-radius: 8px 8px 0 0;
  padding: 8px 10px;
  background: #f7f5ef;
  text-align: left;
}
.hand.active .hand-head { background: var(--accent-soft); }
.hand-title { font-weight: 700; }
.hand-count { color: var(--muted); }
.badge {
  margin-left: auto;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
}
.hand-cards {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 8px;
  padding: 10px;
  flex: 1;
  max-height: 480px;
  overflow-y: auto;
}
.hand-empty { margin: 0; color: var(--muted); font-size: 0.9rem; }

.card {
  position: relative;
  width: var(--hand-card-w);
  padding: 0;
  border: 2px solid transparent;
  border-radius: calc(var(--card-radius) + 2px);
  background: transparent;
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.25);
  transition: transform 0.08s ease;
}
.card:hover:not(:disabled) { background: transparent; transform: translateY(-2px); }
.card.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent), 0 4px 10px rgb(0 0 0 / 0.3);
  transform: translateY(-4px);
}
.card-badge {
  position: absolute;
  top: 4px;
  left: 50%;
  transform: translateX(-50%);
  margin: 0;
  font-size: 0.62rem;
  padding: 1px 6px;
}
.card-desc {
  position: absolute;
  left: 4px; right: 4px; top: 22px;
  padding: 4px 6px;
  border-radius: 5px;
  background: rgb(20 20 20 / 0.82);
  color: #fff;
  font-size: 0.72rem;
  line-height: 1.25;
  text-align: left;
  display: none;
  max-height: 70%;
  overflow: hidden;
}
.card:hover .card-desc, .card:focus-visible .card-desc { display: block; }

/* ---------- Action bar ---------- */

.action-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  position: sticky;
  bottom: calc(8px + env(safe-area-inset-bottom, 0px));
  box-shadow: 0 2px 10px rgb(0 0 0 / 0.08);
}
.action-info { min-width: 0; }
.action-where { color: var(--muted); }
.action-desc { margin: 2px 0 0; color: var(--muted); font-size: 0.9rem; }
.action-buttons { display: flex; flex-wrap: wrap; gap: 8px; }

/* ---------- Dialog ---------- */

dialog {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  max-width: min(440px, calc(100vw - 32px));
  box-shadow: 0 10px 40px rgb(0 0 0 / 0.3);
}
dialog::backdrop { background: rgb(0 0 0 / 0.4); }
dialog p { margin: 0 0 16px; }
.dialog-buttons { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }

/* ---------- Narrow screens ---------- */

@media (max-width: 999px) {
  .hands { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 699px) {
  #app { padding: 10px; gap: 10px; }
  :root { --hand-card-w: 84px; }
  .hand { min-height: 150px; }
}

@media (max-width: 480px) {
  .hands { grid-template-columns: minmax(0, 1fr); }
  .pile { width: 64px; }
  .table { gap: 10px; }
}
