/* Two of You: app styles. The tile-back design is in tile-back.css. */

:root {
  color-scheme: light;
  --bg: #f6efe4;
  --surface: #fffaf2;
  --ink: #2a2733;
  --ink-soft: #5b5664;
  --primary: #2f3e5c;
  --primary-ink: #fffaf2;
  --accent: #b4502c;        /* Finish turn; white text passes 4.5:1 */
  --accent-ink: #ffffff;
  --match: #2f8a5c;
  --line: #ddcfba;
  --focus: #d26a3f;
  --shadow: 0 2px 6px rgba(40, 30, 20, 0.18);
  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-brand: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 18px;
  line-height: 1.4;
  -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

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

h1, h2 { font-family: var(--font-brand); font-weight: 700; margin: 0; }
h1:focus, h2:focus { outline: none; }

/* ---------- Screens ---------- */

.screen {
  padding:
    max(16px, var(--safe-t))
    max(16px, var(--safe-r))
    max(16px, var(--safe-b))
    max(16px, var(--safe-l));
}

.screen-scroll { min-height: 100vh; min-height: 100dvh; }

.column {
  max-width: 640px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.column-wide { max-width: 960px; }

.brand { text-align: center; padding-top: 8px; }
.brand h1 { font-size: clamp(2.2rem, 7vw, 3.4rem); font-style: italic; color: var(--primary); }
.tagline { margin: 4px 0 0; color: var(--ink-soft); }

.brand-tiles {
  position: relative;
  height: 112px;
  display: flex;
  justify-content: center;
  margin-bottom: 8px;
  --tile-size: 96px;
  --tile-radius: 12px;
}
.brand-tile {
  position: relative;
  width: var(--tile-size);
  height: var(--tile-size);
  border-radius: var(--tile-radius);
  box-shadow: var(--shadow);
  margin: 0 -10px;
}
.brand-tile:first-child { transform: rotate(-7deg); }
.brand-tile:last-child { transform: rotate(6deg) translateY(10px); }

.screen-head { text-align: center; }
.screen-head h2 { font-size: clamp(1.8rem, 5vw, 2.4rem); color: var(--primary); }
.sub { margin: 4px 0 0; color: var(--ink-soft); }

/* ---------- Form groups ---------- */

.group { border: 0; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.group legend { font-weight: 700; font-size: 1.05rem; padding: 0; margin-bottom: 10px; }
.group-row { flex-direction: row; justify-content: center; }
.hint { margin: 0; font-size: 0.9rem; color: var(--ink-soft); }

.choices { display: grid; gap: 12px; }
.choices-3 { grid-template-columns: repeat(3, 1fr); }
.choices-4 { grid-template-columns: repeat(4, 1fr); }

.choice { position: relative; display: block; }
.choice input {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  margin: 0; opacity: 0; cursor: pointer;
}
.choice span {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-height: 76px;
  padding: 10px 6px;
  border: 2px solid var(--line);
  border-radius: 16px;
  background: var(--surface);
  font-weight: 700;
  font-size: 1.2rem;
  text-align: center;
  transition: background-color .15s, border-color .15s, color .15s;
}
.choice small { font-weight: 500; font-size: 0.85rem; opacity: 0.85; margin-top: 2px; }
.choice input:checked + span { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.choice input:focus-visible + span { outline: 3px solid var(--focus); outline-offset: 2px; }

.name-fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px; }
.name-fields label { display: flex; flex-direction: column; gap: 4px; font-size: 0.9rem; color: var(--ink-soft); font-weight: 600; }
.name-fields input {
  font: inherit;
  font-size: 1.1rem;
  color: var(--ink);
  min-height: 52px;
  padding: 0 14px;
  border: 2px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
}
.name-fields input:focus { outline: none; border-color: var(--primary); }

/* Switch */
.switch { display: inline-flex; align-items: center; gap: 12px; font-weight: 700; cursor: pointer; min-height: 52px; position: relative; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch-track {
  width: 58px; height: 34px; border-radius: 999px;
  background: #cbbfae; position: relative; transition: background-color .2s;
  flex: none;
}
.switch-track::after {
  content: ""; position: absolute; top: 4px; left: 4px;
  width: 26px; height: 26px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3);
  transition: transform .2s;
}
.switch input:checked + .switch-track { background: var(--match); }
.switch input:checked + .switch-track::after { transform: translateX(24px); }
.switch input:focus-visible + .switch-track { outline: 3px solid var(--focus); outline-offset: 2px; }

/* ---------- Buttons ---------- */

.actions { display: flex; flex-wrap: wrap; gap: 12px; justify-content: center; }

.btn {
  appearance: none;
  -webkit-appearance: none;
  font: inherit;
  font-weight: 700;
  font-size: 1.1rem;
  min-height: 56px;
  padding: 0 26px;
  border-radius: 999px;
  border: 2px solid var(--primary);
  background: var(--surface);
  color: var(--primary);
  cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  touch-action: manipulation;
  user-select: none; -webkit-user-select: none;
}
.btn:focus-visible { outline: 3px solid var(--focus); outline-offset: 3px; }
.btn.primary { background: var(--primary); color: var(--primary-ink); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-quiet { border-color: transparent; background: transparent; }
.btn-wide { min-width: min(320px, 100%); }

.icon-btn {
  appearance: none; border: 0; background: var(--surface);
  width: 52px; height: 52px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--primary); fill: currentColor; cursor: pointer; flex: none;
  box-shadow: var(--shadow); touch-action: manipulation;
}
.icon-btn:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* ---------- Photo picking ---------- */

.status { text-align: center; font-weight: 600; margin: 0; min-height: 1.4em; }
.status.is-ready { color: var(--match); }
.status.is-warn { color: var(--accent); }

.notice {
  background: #fff1e8;
  border: 2px solid #e9b89f;
  border-radius: 14px;
  padding: 14px 18px;
  color: var(--ink);
}
.notice p { margin: 0 0 6px; }
.notice p:last-child { margin-bottom: 0; }
.notice strong { color: var(--accent); }
.notice .files { font-size: 0.9rem; color: var(--ink-soft); word-break: break-word; }

.photo-tray {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  gap: 10px;
}
.photo-tray:empty { display: none; }

.thumb {
  appearance: none; padding: 0; border: 3px solid transparent;
  border-radius: 14px; overflow: hidden; position: relative;
  aspect-ratio: 1 / 1; background: #e7dccb; cursor: pointer;
  touch-action: manipulation;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; transition: opacity .15s, filter .15s; -webkit-touch-callout: none; pointer-events: none; }
.thumb[aria-pressed="false"] img { opacity: 0.4; filter: grayscale(0.8); }
.thumb[aria-pressed="true"] { border-color: var(--primary); }
.thumb .badge {
  position: absolute; top: 6px; right: 6px;
  min-width: 28px; height: 28px; padding: 0 6px; border-radius: 14px;
  background: var(--primary); color: var(--primary-ink);
  font-weight: 700; font-size: 0.9rem;
  display: flex; align-items: center; justify-content: center;
}
.thumb[aria-pressed="false"] .badge { display: none; }
.thumb:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.actions-sticky {
  position: sticky;
  bottom: 0;
  padding: 12px 0 max(4px, var(--safe-b));
  background: linear-gradient(to bottom, rgba(246,239,228,0), var(--bg) 30%);
}

/* ---------- Game ---------- */

.screen-game {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow: hidden;
  padding-top: max(10px, var(--safe-t));
  padding-bottom: max(10px, var(--safe-b));
}

.game-bar { display: flex; align-items: center; gap: 10px; flex: none; }

.score-strip {
  flex: 1; min-width: 0;
  display: flex; gap: 8px; flex-wrap: wrap; align-items: center;
}
.chip {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 6px 8px 6px 16px;
  min-height: 44px;
  border-radius: 999px;
  background: var(--surface);
  border: 2px solid var(--line);
  font-weight: 700;
  white-space: nowrap;
  max-width: 100%;
  transition: background-color .2s, color .2s, border-color .2s;
}
.chip .name { overflow: hidden; text-overflow: ellipsis; }
.chip .count {
  min-width: 32px; height: 30px; padding: 0 8px; border-radius: 999px;
  display: inline-flex; align-items: center; justify-content: center;
  background: rgba(47, 62, 92, 0.1);
}
.chip.is-active { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.chip.is-active .count { background: rgba(255, 255, 255, 0.2); }
.chip-plain { padding-right: 16px; }

.board-wrap {
  flex: 1; min-height: 0;
  display: flex; align-items: center; justify-content: center;
}

.board {
  --tile-size: 100px;
  --gap: 10px;
  --cols: 4;
  --tile-radius: calc(var(--tile-size) * 0.09);
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--tile-size));
  grid-auto-rows: var(--tile-size);
  gap: var(--gap);
}

.tile {
  appearance: none; -webkit-appearance: none;
  border: 0; padding: 0; margin: 0; background: none;
  width: var(--tile-size); height: var(--tile-size);
  border-radius: var(--tile-radius);
  perspective: 900px;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none; -webkit-user-select: none;
}
.tile:focus-visible { outline: 3px solid var(--focus); outline-offset: 3px; }
.tile-inner {
  position: relative; display: block;
  width: 100%; height: 100%;
  transform-style: preserve-3d;
  -webkit-transform-style: preserve-3d;
  transition: transform .42s cubic-bezier(.2, .7, .3, 1);
}
.tile.is-up .tile-inner { transform: rotateY(180deg); }
.tile-face {
  position: absolute; inset: 0;
  border-radius: var(--tile-radius);
  overflow: hidden;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  box-shadow: var(--shadow);
}
.tile-front { transform: rotateY(180deg); background: #e7dccb; }
.tile-front img {
  width: 100%; height: 100%; object-fit: cover; display: block;
  pointer-events: none; -webkit-touch-callout: none; -webkit-user-drag: none;
  transition: opacity .3s;
}
.tile.is-matched, .board.is-locked .tile { cursor: default; }
.tile.is-matched .tile-front img { opacity: 0.8; }
.tile.is-matched .tile-front::after {
  content: "";
  position: absolute; inset: 0;
  border-radius: inherit;
  box-shadow: inset 0 0 0 calc(var(--tile-size) * 0.035 + 2px) var(--match);
}

@media (hover: hover) {
  .board:not(.is-locked) .tile:not(.is-up):hover .tile-inner { transform: translateY(-3px); }
}

.turn-bar {
  flex: none;
  min-height: 76px;
  display: flex; align-items: center; justify-content: center;
}
.turn-msg { margin: 0; font-size: 1.15rem; font-weight: 600; color: var(--ink-soft); text-align: center; }
.btn.finish {
  background: var(--accent); border-color: var(--accent); color: var(--accent-ink);
  font-size: 1.4rem; min-height: 68px; min-width: min(360px, 100%);
  box-shadow: 0 6px 16px rgba(180, 80, 44, 0.35);
  animation: finish-in .25s ease-out;
}
@keyframes finish-in { from { transform: translateY(10px); opacity: 0; } to { transform: none; opacity: 1; } }

/* Short screens (phones in landscape) */
@media (max-height: 500px) {
  .screen-game { gap: 6px; }
  .turn-bar { min-height: 60px; }
  .btn.finish { min-height: 54px; font-size: 1.2rem; }
  .chip { min-height: 38px; padding-top: 3px; padding-bottom: 3px; }
  .icon-btn { width: 44px; height: 44px; }
}

/* ---------- Match reveal ---------- */

.reveal {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  background: rgba(22, 24, 34, 0.74);
  cursor: pointer;
  animation: fade-in .2s ease-out;
  -webkit-tap-highlight-color: transparent;
}
.reveal-card {
  background: #fff;
  padding: 10px;
  border-radius: 18px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
  animation: pop .5s cubic-bezier(.2, 1.4, .4, 1) both;
}
.reveal-card img {
  display: block;
  max-width: calc(min(92vw, 1000px) - 20px);
  max-height: calc(84vh - 20px);
  max-height: calc(84dvh - 20px);
  border-radius: 10px;
}
.confetti { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.confetti i {
  position: absolute; left: 50%; top: 50%;
  width: 10px; height: 14px; border-radius: 2px;
  background: var(--c);
  opacity: 0;
  animation: burst 1.2s cubic-bezier(.1, .75, .3, 1) var(--d) forwards;
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes pop { from { transform: scale(.55) rotate(-5deg); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes burst {
  0%   { transform: translate(-50%, -50%) rotate(0deg); opacity: 1; }
  80%  { opacity: 1; }
  100% { transform: translate(calc(-50% + var(--x)), calc(-50% + var(--y))) rotate(var(--r)); opacity: 0; }
}

/* ---------- End ---------- */

.end-scores {
  list-style: none; margin: 0 auto; padding: 0;
  display: flex; flex-direction: column; gap: 8px;
  width: min(420px, 100%);
}
.end-scores li {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 18px; border-radius: 14px;
  background: var(--surface); border: 2px solid var(--line);
  font-weight: 700; font-size: 1.1rem;
}
.end-scores li.is-winner { border-color: var(--match); }
.end-scores li.is-winner .who::after { content: " ★"; color: #c9932b; }

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 10px;
}
.gallery button {
  appearance: none; border: 0; padding: 0; cursor: pointer;
  aspect-ratio: 1 / 1; border-radius: 12px; overflow: hidden;
  box-shadow: var(--shadow); background: #e7dccb;
}
.gallery img { width: 100%; height: 100%; object-fit: cover; display: block; pointer-events: none; }
.gallery button:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* ---------- Menu dialog ---------- */

.menu-dialog {
  border: 0; border-radius: 22px; padding: 0;
  width: min(520px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  background: var(--bg); color: var(--ink);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.35);
}
.menu-dialog::backdrop { background: rgba(22, 24, 34, 0.55); }
.menu-body { padding: 24px; display: flex; flex-direction: column; gap: 18px; }
.menu-body h2 { color: var(--primary); }

/* ---------- ?debug diagnostics ---------- */

.debug-log {
  position: fixed; left: 8px; right: 8px; top: max(8px, var(--safe-t)); z-index: 100;
  max-height: 30vh; overflow: auto; margin: 0;
  padding: 10px 12px; border-radius: 10px;
  background: rgba(20, 20, 28, 0.9); color: #d8f5d0;
  font: 12px/1.45 ui-monospace, Menlo, monospace;
  white-space: pre-wrap; word-break: break-word;
  -webkit-user-select: text; user-select: text;
}

/* ---------- Small screens ---------- */

@media (max-width: 420px) {
  body { font-size: 17px; }
  .choice span { min-height: 68px; font-size: 1.1rem; }
  .choice small { font-size: 0.75rem; }
  .btn { padding: 0 20px; }
}

/* ---------- Reduced motion ---------- */

@media (prefers-reduced-motion: reduce) {
  .tile-inner { transition: none; }
  .board:not(.is-locked) .tile:not(.is-up):hover .tile-inner { transform: none; }
  .tile.is-up .tile-inner { transform: rotateY(180deg); }
  .reveal, .reveal-card, .btn.finish { animation: none; }
  .confetti { display: none; }
  .choice span, .switch-track, .switch-track::after, .chip { transition: none; }
}
