/* =========================================================================
   Daniela's Mahjong — design system

   Flat surfaces, hairline rules, one typeface. The tiles are the only
   saturated thing on the screen; everything around them stays near-neutral.
   Colour values all come from the chosen table style (js/theme.js).
   ========================================================================= */
:root {
  --font-ui: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  --font-cjk: 'Hiragino Mincho ProN', 'Songti SC', 'PingFang SC', 'Noto Serif CJK SC', 'MS Mincho', serif;

  /* Bone defaults, replaced on first paint. */
  --bg-main: #ebe6dc;
  --surface: #f5f2ea;
  --surface-2: #fdfbf6;
  --line: rgba(42, 38, 32, .11);
  --line-2: rgba(42, 38, 32, .2);
  --ink: #2a2620;
  --ink-2: #6b6458;
  --ink-3: #9a9286;
  --accent: #8a5340;
  --accent-ink: #fdfbf6;
  --accent-soft: rgba(138, 83, 64, .12);
  --accent-line: rgba(138, 83, 64, .45);
  --felt: #fbf9f4;
  --felt-ink: #2a2620;
  --felt-ink-2: #9a9286;
  --felt-line: rgba(42, 38, 32, .12);
  --back: #cdc4b3;
  --shadow-tile: 0 1px 1px rgba(40, 34, 26, .07), 0 2px 5px rgba(40, 34, 26, .07);
  --shadow-pop: 0 16px 40px rgba(40, 34, 26, .18);
  --scrim: rgba(40, 34, 26, .4);

  --r-lg: 12px;
  --r-md: 10px;
  --r-sm: 7px;

  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: 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: var(--font-ui);
  font-size: 15px;
  color: var(--ink);
  background: var(--bg-main);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  user-select: none;
}

button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
svg { width: 100%; height: 100%; display: block; }

/* ---------- screens ------------------------------------------------------ */
.screen {
  position: fixed; inset: 0; z-index: 1;
  display: none;
  flex-direction: column;
  padding-top: var(--safe-t);
  padding-bottom: var(--safe-b);
}
body[data-screen='home']     .screen--home,
body[data-screen='game']     .screen--game,
body[data-screen='card']     .screen[data-name='card'],
body[data-screen='theme']    .screen[data-name='theme'],
body[data-screen='learn']    .screen[data-name='learn'],
body[data-screen='stats']    .screen[data-name='stats'],
body[data-screen='settings'] .screen[data-name='settings'] { display: flex; animation: fade .2s ease; }

@keyframes fade { from { opacity: 0; } }

/* =========================================================================
   HOME
   ========================================================================= */
.home-inner {
  flex: 1; min-height: 0;
  display: flex; flex-direction: column; align-items: center;
  padding: 20px 24px 16px;
  overflow: auto;
}

.logo-tiles {
  display: flex; align-items: flex-end; justify-content: center;
  gap: 7px; margin-top: clamp(24px, 9vh, 78px);
}
.logo-tiles .tile { width: 52px; }
.logo-tiles .tile:nth-child(2) { width: 60px; }

.wordmark {
  font-size: 13.5px; font-weight: 600;
  letter-spacing: .2em; text-transform: uppercase;
  text-align: center; margin: 30px 0 0; color: var(--ink);
}
.rule { width: 26px; height: 1px; background: var(--line-2); margin: 16px 0 0; }

.menu { width: 100%; max-width: 420px; margin-top: clamp(30px, 7vh, 56px); }

.menu-item {
  width: 100%;
  display: flex; align-items: center; gap: 14px;
  padding: 16px 4px;
  border: 0; border-bottom: 1px solid var(--line);
  background: none; text-align: left;
  transition: opacity .15s ease;
}
.menu-item:first-child { border-top: 1px solid var(--line); }
.menu-item:active { opacity: .5; }
.menu-ico { width: 19px; height: 19px; flex: none; color: var(--ink-3); }
.menu-ico svg { fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }
.menu-text { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.menu-text b { font-size: 15.5px; font-weight: 500; letter-spacing: -.005em; }
.menu-text i { font-style: normal; font-size: 12.5px; color: var(--ink-3); }
.menu-arrow { color: var(--ink-3); font-size: 17px; line-height: 1; opacity: .7; }

.home-foot {
  margin-top: auto; padding-top: 28px;
  width: 100%; max-width: 420px;
  display: flex; align-items: center; justify-content: space-between;
}
.ghost-btn { width: 36px; height: 36px; padding: 9px; color: var(--ink-3); }
.ghost-btn svg { fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }
.ghost-btn:active { color: var(--ink); }

/* =========================================================================
   TILES
   ========================================================================= */
.tile {
  position: relative;
  width: 100%;
  aspect-ratio: 100 / 134;
  border-radius: 9%/6.7%;
  background: #fff;
  box-shadow: var(--shadow-tile);
  overflow: hidden;
  transition: transform .14s ease, box-shadow .14s ease;
}
.tile .tile-svg { position: absolute; inset: 0; width: 100%; height: 100%; }

.tile--sm { box-shadow: 0 1px 1.5px rgba(40, 34, 26, .1); }
.tile--back { background: var(--back); box-shadow: none; }

/* =========================================================================
   GAME · top bar
   ========================================================================= */
.topbar {
  flex: none;
  display: grid; grid-template-columns: 36px 1fr 36px; align-items: center;
  gap: 8px; padding: 10px 14px;
}
.topbar-title { text-align: center; min-width: 0; }
.topbar-title b { display: block; font-size: 14px; font-weight: 600; letter-spacing: .01em; }
.topbar-title i {
  display: block; font-style: normal; font-size: 10px; color: var(--ink-3);
  letter-spacing: .13em; text-transform: uppercase; margin-top: 3px;
}
.icon-btn { width: 36px; height: 36px; padding: 9px; color: var(--ink-2); }
.icon-btn svg { fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn:active { color: var(--ink); }
.icon-btn.ghost { visibility: hidden; }

/* =========================================================================
   GAME · opponents
   ========================================================================= */
.opponents { flex: none; display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; padding: 0 14px 10px; }
.opp {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-md);
  padding: 8px 9px 9px; min-width: 0;
  transition: border-color .2s ease, background .2s ease;
}
.opp.is-turn { border-color: var(--accent-line); background: var(--surface-2); }
.opp-top { display: flex; align-items: center; gap: 6px; min-width: 0; }
.opp-wind {
  flex: none; width: 15px; height: 15px; border-radius: 4px;
  background: var(--line); color: var(--ink-2);
  font-size: 9px; font-weight: 600; display: grid; place-items: center;
}
.opp.is-turn .opp-wind { background: var(--accent); color: var(--accent-ink); }
.opp-name { font-size: 12px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.opp-rack { display: flex; gap: 1.5px; margin-top: 7px; height: 11px; }
.opp-rack span { flex: 1; border-radius: 1.5px; background: var(--back); }
.opp-melds { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.opp-melds .meld { display: flex; gap: 1px; }
.opp-melds .tile { width: 12px; }

/* =========================================================================
   GAME · table
   ========================================================================= */
.table { flex: 1; min-height: 0; display: flex; flex-direction: column; padding: 0 14px 12px; }

.table-felt {
  position: relative; flex: 1; min-height: 0;
  border-radius: var(--r-lg);
  background: var(--felt);
  border: 1px solid var(--felt-line);
  display: grid; grid-template-rows: auto minmax(0, 1fr) auto;
  overflow: hidden;
  color: var(--felt-ink);
  padding: 14px 12px 24px;
}
.table-felt.is-empty { grid-template-rows: 1fr auto 1fr; align-items: center; }
.table-felt.is-empty .table-status { align-self: end; }
.table-felt.is-empty .table-actions { align-self: start; }

.felt-pool {
  display: flex; flex-wrap: wrap; align-content: center; justify-content: center;
  gap: 3px; padding: 10px 2px 6px;
  overflow-y: auto; scrollbar-width: none;
}
.felt-pool::-webkit-scrollbar { display: none; }
.felt-pool .tile { width: 27px; }
.felt-pool .tile.is-latest {
  margin: 2px 4px; transform: scale(1.16);
  box-shadow: 0 0 0 1.5px var(--accent);
  animation: drop .24s ease;
}
@keyframes drop { from { opacity: 0; transform: translateY(-10px) scale(.9); } }

.table-status {
  margin: 0 auto; text-align: center; max-width: 17em;
  font-size: 15px; font-weight: 500; color: var(--felt-ink); line-height: 1.4;
}
.table-status small {
  display: block; font-size: 12.5px; font-weight: 400;
  color: var(--felt-ink-2); margin-top: 5px;
}
.table-actions { display: flex; flex-wrap: wrap; gap: 7px; justify-content: center; }
.table-actions:empty { display: none; }

.felt-btn {
  padding: 9px 15px; border-radius: var(--r-sm);
  border: 1px solid var(--felt-line); color: var(--felt-ink);
  font-size: 13px; font-weight: 500;
  transition: opacity .14s ease;
}
.felt-btn:active { opacity: .55; }
.felt-btn--go { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.felt-btn--quiet { color: var(--felt-ink-2); border-color: transparent; }

.wall-count {
  position: absolute; right: 14px; bottom: 8px;
  font-size: 10.5px; color: var(--felt-ink-2); letter-spacing: .04em;
}
.wall-count span { font-weight: 600; }

.pass-tray { display: flex; gap: 9px; align-items: center; justify-content: center; }
.pass-tray .tile { width: 46px; animation: drop .22s ease; }
.pass-tray .tray-slot {
  width: 46px; aspect-ratio: 100 / 134; border-radius: var(--r-sm);
  border: 1px dashed var(--felt-line);
}

/* =========================================================================
   GAME · your rack
   ========================================================================= */
.rack-area { flex: none; padding: 0 14px 12px; }
.rack-head { display: flex; align-items: center; gap: 8px; min-height: 30px; margin-bottom: 9px; }
.me-chip {
  flex: none; display: flex; align-items: center; gap: 6px;
  padding: 5px 9px 5px 6px; border-radius: var(--r-sm);
  border: 1px solid var(--line);
  font-size: 12px; font-weight: 500;
  max-width: 42%; white-space: nowrap; overflow: hidden;
}
.me-chip span:last-child { overflow: hidden; text-overflow: ellipsis; }
.me-chip.is-turn { border-color: var(--accent-line); }
.me-chip.is-turn .opp-wind { background: var(--accent); color: var(--accent-ink); }
.my-exposures { flex: 1; display: flex; gap: 6px; flex-wrap: wrap; min-width: 0; }
.my-exposures .meld { display: flex; gap: 1.5px; }
.my-exposures .tile { width: 19px; }

.pill-btn {
  flex: none; display: flex; align-items: center; gap: 6px;
  padding: 6px 11px; border-radius: var(--r-sm);
  border: 1px solid var(--line);
  font-size: 12px; font-weight: 500; color: var(--ink-2);
}
.pill-btn svg { width: 13px; height: 13px; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }
.pill-btn:active { color: var(--ink); }

.rack { display: grid; grid-template-columns: repeat(7, 1fr); gap: 5px; }
.rack .slot { position: relative; }
.rack .tile { cursor: pointer; }
.rack .tile.is-selected { transform: translateY(-9px); box-shadow: 0 6px 14px rgba(40, 34, 26, .16), 0 0 0 1.5px var(--accent); z-index: 3; }
.rack .slot.is-new::after {
  content: ''; position: absolute; top: -5px; left: 50%; transform: translateX(-50%);
  z-index: 4; width: 4px; height: 4px; border-radius: 50%; background: var(--accent);
}
.rack .tile.is-dim { opacity: .38; }
.rack .tile.is-marked { box-shadow: var(--shadow-tile), 0 0 0 1.5px var(--accent-line); }

.action-bar { display: flex; gap: 8px; margin-top: 12px; min-height: 46px; }
.act {
  flex: 1; height: 46px; border-radius: var(--r-md);
  display: grid; place-items: center;
  font-size: 14px; font-weight: 500;
  border: 1px solid var(--line); color: var(--ink);
  transition: opacity .14s ease;
}
.act:active { opacity: .6; }
/* A disabled primary drops the fill rather than fading it — a washed-out
   tint of the accent reads as a mistake, an outline reads as "not yet". */
.act[disabled] { background: none; border-color: var(--line); color: var(--ink-3); }
.act--primary, .act--win { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.act--jade { background: none; border-color: var(--line); color: var(--ink); }

/* =========================================================================
   SHEETS
   ========================================================================= */
.sheet-scroll { flex: 1; min-height: 0; overflow-y: auto; padding: 4px 20px 48px; -webkit-overflow-scrolling: touch; }

.sec-title {
  font-size: 10.5px; font-weight: 600;
  letter-spacing: .16em; text-transform: uppercase; color: var(--ink-3);
  margin: 30px 0 12px;
}
.sec-title:first-child { margin-top: 10px; }

.card-hand { padding: 15px 0; border-bottom: 1px solid var(--line); }
.card-hand-top { display: flex; align-items: baseline; gap: 9px; margin-bottom: 11px; }
.card-hand-pat { font-size: 14px; font-weight: 600; letter-spacing: .05em; flex: 1; }
.card-hand-pts { font-size: 11.5px; font-weight: 500; color: var(--ink-3); }
.card-hand-tag {
  font-size: 9.5px; font-weight: 600; letter-spacing: .1em; text-transform: uppercase;
  color: var(--ink-3); border: 1px solid var(--line); border-radius: 3px; padding: 2px 6px;
}
.card-hand-tiles { display: flex; flex-wrap: wrap; gap: 3px; }
.card-hand-tiles .grp { display: flex; gap: 1.5px; margin-right: 7px; }
.card-hand-tiles .tile { width: 23px; }
.card-hand-note { font-size: 12.5px; color: var(--ink-3); margin: 11px 0 0; line-height: 1.5; }

.prose { font-size: 14.5px; line-height: 1.65; color: var(--ink-2); }
.prose p { margin: 0 0 14px; }
.prose b, .prose strong { color: var(--ink); font-weight: 600; }
.prose ul { margin: 0 0 14px; padding-left: 18px; }
.prose li { margin-bottom: 7px; }

.setting {
  display: flex; align-items: center; gap: 14px;
  padding: 15px 0; border-bottom: 1px solid var(--line);
}
.setting-text { flex: 1; min-width: 0; }
.setting-text b { display: block; font-size: 14.5px; font-weight: 500; }
.setting-text i { font-style: normal; font-size: 12.5px; color: var(--ink-3); }
.switch { flex: none; width: 42px; height: 24px; border-radius: 12px; background: var(--line-2); position: relative; transition: background .2s ease; }
.switch::after { content: ''; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: var(--surface-2); transition: transform .2s ease; }
.switch[aria-checked='true'] { background: var(--accent); }
.switch[aria-checked='true']::after { transform: translateX(18px); }

.seg { display: flex; border: 1px solid var(--line); border-radius: var(--r-sm); overflow: hidden; }
.seg button { padding: 7px 11px; font-size: 12px; font-weight: 500; color: var(--ink-3); border-right: 1px solid var(--line); }
.seg button:last-child { border-right: 0; }
.seg button[aria-pressed='true'] { background: var(--accent); color: var(--accent-ink); }

.name-input {
  flex: none; width: 48%; min-width: 0;
  padding: 9px 11px; border-radius: var(--r-sm);
  background: none; border: 1px solid var(--line);
  color: var(--ink); font-family: inherit; font-size: 14px; font-weight: 500;
  text-align: right; -webkit-user-select: text; user-select: text;
}
.name-input:focus { outline: none; border-color: var(--accent); }
.name-input::placeholder { color: var(--ink-3); font-weight: 400; }

.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 1px; background: var(--line); border: 1px solid var(--line); border-radius: var(--r-md); overflow: hidden; }
.stat-box { background: var(--bg-main); padding: 20px 14px; text-align: center; }
.stat-box b { display: block; font-size: 27px; font-weight: 400; letter-spacing: -.02em; }
.stat-box i { font-style: normal; font-size: 10px; letter-spacing: .13em; text-transform: uppercase; color: var(--ink-3); }

/* ---------- table style picker ------------------------------------------- */
.theme-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px; }
.theme-card {
  position: relative; text-align: left; padding: 0 0 12px;
  border-radius: var(--r-md); overflow: hidden;
  border: 1px solid var(--line);
  display: flex; flex-direction: column;
  transition: border-color .2s ease;
}
.theme-card.is-on { border-color: var(--accent); }
.theme-prev { position: relative; height: 100px; display: grid; place-items: center; }
.theme-prev-mat { position: absolute; left: 13px; right: 13px; top: 12px; bottom: 12px; border-radius: var(--r-sm); }
.theme-prev-tiles { position: relative; display: flex; gap: 4px; }
.theme-prev-tiles .tile { width: 25px; box-shadow: 0 1px 2px rgba(20, 18, 14, .14); }
.theme-meta { padding: 11px 13px 0; }
.theme-meta b { display: block; font-size: 14px; font-weight: 500; }
.theme-meta i { font-style: normal; font-size: 11.5px; color: var(--ink-3); }
.theme-tick {
  position: absolute; top: 9px; right: 9px; width: 18px; height: 18px; border-radius: 50%;
  background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; font-size: 10px; font-weight: 700;
}

/* =========================================================================
   MODALS
   ========================================================================= */
.modal-root { position: fixed; inset: 0; z-index: 60; display: flex; align-items: flex-end; justify-content: center; }
.modal-root::before { content: ''; position: absolute; inset: 0; background: var(--scrim); animation: fade .18s ease; }

.modal {
  position: relative; width: 100%; max-width: 500px;
  max-height: 86vh; display: flex; flex-direction: column;
  background: var(--bg-main);
  border-top: 1px solid var(--line);
  border-radius: var(--r-lg) var(--r-lg) 0 0;
  box-shadow: var(--shadow-pop);
  padding-bottom: var(--safe-b);
  animation: up .24s ease;
}
@keyframes up { from { transform: translateY(24px); opacity: 0; } }
.modal--center { border-radius: var(--r-lg); margin: auto 16px; border: 1px solid var(--line); }
.modal-grip { width: 30px; height: 3px; border-radius: 2px; background: var(--line-2); margin: 10px auto 0; flex: none; }
.modal-head { padding: 18px 20px 6px; flex: none; }
.modal-head h2 { font-size: 17px; font-weight: 600; margin: 0; letter-spacing: -.005em; }
.modal-head p { margin: 5px 0 0; font-size: 13px; color: var(--ink-3); line-height: 1.5; }
.modal-body { flex: 1; min-height: 0; overflow-y: auto; padding: 10px 20px 4px; }
.modal-foot { flex: none; display: flex; gap: 8px; padding: 14px 20px 18px; }

.suggest-row { padding: 14px 0; border-bottom: 1px solid var(--line); }
.suggest-top { display: flex; align-items: center; gap: 9px; margin-bottom: 9px; }
.suggest-top b { font-size: 13.5px; font-weight: 600; letter-spacing: .05em; flex: 1; }
.need-chip { font-size: 11px; font-weight: 500; color: var(--ink-3); white-space: nowrap; }
.suggest-tiles { display: flex; flex-wrap: wrap; gap: 3px; }
.suggest-tiles .grp { display: flex; gap: 1.5px; margin-right: 7px; }
.suggest-tiles .tile { width: 22px; }
.suggest-tiles .tile.missing { opacity: .25; }

.result-hero { text-align: center; padding: 10px 0 4px; }
.result-hero .big { font-size: 21px; font-weight: 600; margin: 0; letter-spacing: -.01em; }
.result-hero .sub { font-size: 13px; color: var(--ink-3); margin: 6px 0 16px; }
.result-tiles { display: flex; flex-wrap: wrap; gap: 3px; justify-content: center; margin-bottom: 12px; }
.result-tiles .grp { display: flex; gap: 1.5px; margin: 0 4px 4px; }
.result-tiles .tile { width: 24px; }

/* ---------- toast --------------------------------------------------------- */
.toast {
  position: fixed; left: 50%; bottom: calc(30% + var(--safe-b)); transform: translateX(-50%);
  z-index: 80; padding: 9px 16px; border-radius: var(--r-sm);
  background: var(--ink); color: var(--bg-main);
  font-size: 12.5px; font-weight: 500;
  box-shadow: var(--shadow-pop); animation: fade .18s ease;
  max-width: 84vw; text-align: center;
}

/* ---------- portrait guard ------------------------------------------------ */
.rotate-nudge { display: none; }
@media (orientation: landscape) and (max-height: 560px) {
  .rotate-nudge {
    position: fixed; inset: 0; z-index: 200; display: grid; place-items: center;
    background: var(--bg-main); text-align: center; padding: 24px;
  }
  .rotate-ico { width: 34px; height: 34px; margin: 0 auto 16px; color: var(--ink-3); }
  .rotate-ico svg { fill: none; stroke: currentColor; stroke-width: 1.4; stroke-linecap: round; }
  .rotate-nudge p { margin: 0; font-size: 15px; font-weight: 500; }
  .rotate-nudge .dim { font-size: 13px; color: var(--ink-3); margin-top: 6px; max-width: 24em; }
}

/* ---------- small phones -------------------------------------------------- */
@media (max-height: 700px) {
  .rack { gap: 4px; }
  .action-bar { margin-top: 9px; min-height: 43px; }
  .act { height: 43px; }
  .table-status { font-size: 14px; }
  .felt-pool .tile { width: 24px; }
}
@media (max-height: 620px) {
  .opponents { padding-bottom: 7px; }
  .opp-rack { height: 9px; }
  .rack-head { min-height: 27px; margin-bottom: 7px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
