/* Lore counter — mobile landscape, 2v2 face-to-face layout */

html, body { height: 100%; overscroll-behavior: none; }

body.lore-page {
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  /* safe-area for notched devices in landscape */
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
           env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* Portrait hint is unused now — the viewport-rotate rule below forces a
   landscape presentation even when the device is upright. */
.lore-rotate-hint { display: none; }

/* Force-landscape on narrow portrait viewports: rotate the whole page 90°
   so the landscape grid (2P side-by-side / 4P 2×2) stays visually correct
   regardless of device orientation. Swap width/height so the rotated body
   fills the viewport exactly.
   Rationale: auto-rotate often fails when the phone is lying flat on the
   table — which is exactly how this tool is used. */
@media (orientation: portrait) and (max-width: 900px) {
  html { overflow: hidden; }
  body.lore-page {
    position: fixed;
    top: 50%;
    left: 50%;
    width: 100vh;
    height: 100vw;
    /* base.css sets min-height:100vh — in rotated portrait that stretches
       body past 100vw, pushing the app off-screen. Reset it. */
    min-height: 0;
    transform: translate(-50%, -50%) rotate(90deg);
    transform-origin: center;
    padding: 0;
    display: flex;
    flex-direction: column;
  }
  /* Inside the rotated body, 100dvh still refers to the real (portrait)
     viewport — which is taller than body itself. Use flex:1 so the app
     fills the remaining height of body (= 100vw pre-rotation) instead. */
  body.lore-page .lore-app {
    height: auto;
    flex: 1;
    min-height: 0;
  }
}

/* ---- App layout ----
   Both modes share the same top-level grid. Rows (.lore-row) are dissolved
   via `display: contents` so the panels participate directly in the app
   grid — letting us vary rows vs. columns between 2P and 4P cleanly.
   The app fills the remaining viewport after the shared header. */
.lore-app {
  display: grid;
  height: calc(100dvh - var(--header-h, 40px));
  gap: 2px;
  background: var(--border);
}
.lore-row { display: contents; }

/* --- 2P: side-by-side (P1 left, P2 right, both upright) --- */
body[data-mode="2p"] .lore-app {
  grid-template-columns: 1fr 1fr;
  grid-template-areas: "p1 p2";
}
body[data-mode="2p"] .lore-panel[data-pos="bottom-left"] { grid-area: p1; }
body[data-mode="2p"] .lore-panel[data-pos="top-left"]    { grid-area: p2; }

/* --- 4P: 2×2 face-to-face (top row rotated 180°) --- */
body[data-mode="4p"] .lore-app {
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  grid-template-areas:
    "tl tr"
    "bl br";
}
body[data-mode="4p"] .lore-panel[data-pos="top-left"]     { grid-area: tl; transform: rotate(180deg); }
body[data-mode="4p"] .lore-panel[data-pos="top-right"]    { grid-area: tr; transform: rotate(180deg); }
body[data-mode="4p"] .lore-panel[data-pos="bottom-left"]  { grid-area: bl; }
body[data-mode="4p"] .lore-panel[data-pos="bottom-right"] { grid-area: br; }

/* ---- Panel ---- */
.lore-panel {
  position: relative;
  background: var(--surface);
  display: grid;
  grid-template-rows: auto 1fr auto;
  align-items: center;
  justify-items: center;
  padding: 8px 12px;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
  transition: background 0.25s ease;
}
/* Explicit [hidden] — .lore-panel's display: grid would otherwise override
   the UA stylesheet and leak the hidden panel into the grid. */
.lore-panel[hidden] { display: none; }

.lore-panel.win {
  background: color-mix(in srgb, var(--blue) 22%, var(--surface));
}

/* Win celebration: pulse the panel + expand a ring once on <20 → ≥20.
   Ring uses ::after positioned absolute; panel has overflow:hidden so the
   ring is clipped to the panel bounds (prevents bleeding across the grid). */
@keyframes lore-win-pulse {
  0%   { transform: scale(1);    box-shadow: 0 0 0 0 color-mix(in srgb, var(--blue) 60%, transparent); }
  40%  { transform: scale(1.03); box-shadow: 0 0 0 12px color-mix(in srgb, var(--blue) 0%, transparent); }
  100% { transform: scale(1);    box-shadow: 0 0 0 0  transparent; }
}
@keyframes lore-win-ring {
  0%   { opacity: 0.8; transform: translate(-50%, -50%) scale(0.2); }
  100% { opacity: 0;   transform: translate(-50%, -50%) scale(2.4); }
}
.lore-panel.celebrating {
  animation: lore-win-pulse 0.9s ease-out 2;
}
.lore-panel.celebrating::after {
  content: "";
  position: absolute;
  top: 50%; left: 50%;
  width: 60%;
  aspect-ratio: 1;
  border: 3px solid var(--blue);
  border-radius: 50%;
  pointer-events: none;
  animation: lore-win-ring 1.2s ease-out forwards;
}
/* Rotated panels: the pulse's scale() would clobber rotate(180deg), so
   skip the pulse there (the ::after ring still plays). */
body[data-mode="4p"] .lore-panel[data-pos="top-left"].celebrating,
body[data-mode="4p"] .lore-panel[data-pos="top-right"].celebrating {
  animation: none;
}

.lore-name {
  color: var(--text-mid);
  font-size: 1.05rem;
  font-weight: 600;
  padding: 4px 10px;
  max-width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Big tap-to-increment number. Scales with viewport height so it stays
   readable across phone sizes in landscape. */
.lore-value {
  background: transparent;
  border: none;
  color: var(--text);
  /* Cap on min-side (vmin) so tiny landscape phones don't overflow the row. */
  font-size: clamp(3.5rem, 26vmin, 11rem);
  font-weight: 700;
  line-height: 1;
  width: 100%;
  height: 100%;
  min-width: 0;
  cursor: pointer;
  font-variant-numeric: tabular-nums;
  transition: color 0.2s ease, transform 0.1s ease;
  -webkit-tap-highlight-color: transparent;
}
.lore-value:active { transform: scale(0.97); }
.lore-panel.win .lore-value { color: var(--blue); }

.lore-btns {
  display: flex;
  gap: 8px;
  width: 100%;
  justify-content: center;
}

.lore-btn {
  flex: 1;
  max-width: 160px;
  min-height: 52px;
  background: transparent;
  border: 1px solid var(--border2);
  color: var(--text);
  font-size: 1.8rem;
  font-weight: 600;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition);
  -webkit-tap-highlight-color: transparent;
}
.lore-btn:hover  { border-color: var(--accent); }
.lore-btn:active { background: color-mix(in srgb, var(--accent) 15%, transparent); }
.lore-btn:disabled { opacity: 0.4; cursor: default; }

.lore-btn-plus  { border-color: var(--blue); color: var(--blue); }
.lore-btn-minus { border-color: var(--border2); color: var(--text-mid); }

/* Win badge: hidden by default, shown when panel.win */
.lore-win-badge {
  position: absolute;
  top: 8px;
  right: 12px;
  font-size: 0.9rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  color: var(--blue);
  border: 1px solid var(--blue);
  padding: 2px 6px;
  border-radius: var(--radius-sm);
  opacity: 0;
  transition: opacity 0.25s ease;
  pointer-events: none;
}
.lore-panel.win .lore-win-badge { opacity: 1; }

/* Turn-order badge: click to mark this player as 先攻. Clicking the current
   先攻 unsets. Muted "後攻" style applied to non-first players once any
   player is set. */
.lore-turn-badge {
  position: absolute;
  top: 8px;
  left: 12px;
  font-size: 0.9rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  background: transparent;
  border: 1px dashed var(--border2);
  color: var(--text-dim);
  padding: 4px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition), color var(--transition);
  -webkit-tap-highlight-color: transparent;
}
.lore-turn-badge:hover { border-color: var(--accent); color: var(--text); }

.lore-panel.turn-first .lore-turn-badge {
  background: var(--accent);
  border: 1px solid var(--accent);
  color: #fff;
}
.lore-panel.turn-second .lore-turn-badge {
  border: 1px solid var(--border2);
  color: var(--text-mid);
  background: transparent;
}

/* ---- Modals ---- */
.lore-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
  /* Keep the dialog clear of the home indicator / notch / URL bar on every
     edge. Using max() per side means the same rule works both upright and
     after the rotated-portrait hack (where physical/logical edges swap). */
  padding:
    max(16px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    max(16px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
}
.lore-overlay.active { display: flex; }

.lore-dialog {
  background: var(--surface);
  border: 1px solid var(--border2);
  border-radius: var(--radius-lg);
  padding: 20px;
  width: min(360px, 100%);
  max-height: 100%;
  display: flex;
  flex-direction: column;
  gap: 12px;
  animation: dialog-in 0.18s ease;
}

/* Compact dialog for short viewports (landscape phones) AND the rotated-
   portrait hack (where the rotated body's usable height = 100vw). Targets
   the 4P dice modal, which is the tallest of the three. */
@media (max-height: 520px),
       (orientation: portrait) and (max-width: 900px) {
  .lore-dialog { padding: 12px 16px; gap: 8px; }
  .lore-dialog-title { font-size: 1rem; }
  .lore-dialog-desc  { font-size: 0.85rem; }
  .lore-dice-row { padding: 4px 0; gap: 8px; }
  .lore-die      { padding: 6px 4px; }
  .lore-die-label { font-size: 0.8rem; margin-bottom: 2px; }
  .lore-die-value { font-size: 0.85rem; margin-top: 0; }
  .lore-die-face  { font-size: clamp(1.9rem, 8vmin, 2.6rem); }
  .lore-die-faces { gap: 4px; }
  .lore-btn { min-height: 40px; font-size: 1.4rem; }
}

.lore-dialog-title { font-size: 1.2rem; color: var(--text); }
.lore-dialog-desc  { font-size: 1rem; color: var(--text-mid); }

.lore-input {
  background: var(--bg);
  border: 1px solid var(--border2);
  color: var(--text);
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  font-size: 1.2rem;
  width: 100%;
}
.lore-input:focus { outline: none; border-color: var(--accent); }

.lore-dialog-btns {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  flex-wrap: wrap;
  flex-shrink: 0;  /* Always keep action buttons visible. */
}

/* ---- Dice modal ---- */
.lore-dice-dialog { width: min(560px, 100%); }

/* Grid instead of flex: 2P → 1×2, 4P → 2×2. Uniform cell width avoids the
   flex-wrap reflow where the last die jumps to its own row at odd widths. */
.lore-dice-row {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  padding: 4px 0;
  justify-items: stretch;
  /* Allow the grid to shrink inside a height-constrained dialog. */
  min-height: 0;
}

.lore-die {
  min-width: 0;
  text-align: center;
  background: var(--bg);
  border: 1px solid var(--border2);
  border-radius: var(--radius-md);
  padding: 8px 6px;
  transition: border-color 0.25s ease, background 0.25s ease;
}

.lore-die-label {
  font-size: 0.85rem;
  color: var(--text-mid);
  margin-bottom: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Two dice per player sit side-by-side. Gap keeps the shake anim from
   visually overlapping. */
.lore-die-faces {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 6px;
}

/* Unicode dice faces U+2680–U+2685 — larger line-height keeps shake anim
   from being clipped at the top. */
.lore-die-face {
  /* Dice are the hero of this dialog — sized large enough to dominate the
     cell while still fitting a 4P 2×2 grid inside short landscape phones. */
  font-size: clamp(2.2rem, 9.5vmin, 3.2rem);
  line-height: 1.2;
  color: var(--text);
  display: inline-block;
}

.lore-die-value {
  font-size: 0.9rem;
  color: var(--text-dim);
  margin-top: 0;
  min-height: 1em;
}

.lore-die.winner {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, var(--bg));
}
.lore-die.winner .lore-die-face { color: var(--accent); }
.lore-die.winner .lore-die-value { color: var(--accent); font-weight: 700; }

.lore-die.tie { border-color: var(--text-dim); }

@keyframes die-shake {
  0%, 100% { transform: translateY(0)    rotate(0deg); }
  25%      { transform: translateY(-4px) rotate(-10deg); }
  50%      { transform: translateY(2px)  rotate(8deg); }
  75%      { transform: translateY(-2px) rotate(-4deg); }
}
.lore-die.rolling .lore-die-face {
  display: inline-block;
  animation: die-shake 0.18s ease-in-out infinite;
}
/* Desync the two dice for a more organic roll. */
.lore-die.rolling .lore-die-face:nth-child(2) {
  animation-delay: -0.09s;
}
