:root {
  /* A landscape phone's notch sits on one side. */
  --sl: max(8px, env(safe-area-inset-left));
  --sr: max(8px, env(safe-area-inset-right));
  --ui: "Fredoka", ui-rounded, system-ui, sans-serif;
  --read: "Andika", "Comic Sans MS", system-ui, sans-serif;
  --ink: #1d1d1f;
  --paper: #fbf7ec;
  --panel: #3a5055;
  --panel-edge: #1e2b2e;
  --p0: #4dabf7;
  --p1: #ff6b6b;
  --p2: #ffa94d;
  --p3: #b197fc;
  --cw: 60px;
  --ch: 84px;
  color-scheme: dark;
}

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #0d2622;
  color: #fff;
  font-family: var(--ui);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

/* iOS 26+ home-screen apps with the see-through status bar lay the page out
   scrolled by the status bar's height (WebKit bug 301108): fixed layers stop
   short of the bottom and touches land off by that much. A page at least
   screen-tall gets the whole screen. min() is a no-op where the inset is 0,
   and display-mode doesn't match here without a manifest, so no media query. */
html { min-height: min(100lvh, calc(100% + env(safe-area-inset-top))); }

#bg {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  z-index: 0;
}

/* Spans the width between the side safe areas; the layout reads it. */
#safe {
  position: fixed;
  top: 0;
  height: 0;
  left: env(safe-area-inset-left);
  right: env(safe-area-inset-right);
  visibility: hidden;
  pointer-events: none;
}

/* A faint CRT texture over everything. */
body::after {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 50;
  background: repeating-linear-gradient(0deg, rgba(0, 0, 0, 0.04) 0 1px, transparent 1px 3px);
}

#labels, #table { position: fixed; inset: 0; pointer-events: none; }
/* The win cascade's canvas: over the cards, under the bars. */
#cascade { position: fixed; inset: 0; width: 100%; height: 100%; z-index: 4; pointer-events: none; }
#labels { z-index: 1; }
#table { z-index: 2; }

/* ------------------------------------------------------------- cards */

.card {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--cw);
  height: var(--ch);
  pointer-events: auto;
  touch-action: none;
  will-change: transform;
  cursor: pointer;
}

.card .inner {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
}

.face {
  position: absolute;
  inset: 0;
  border-radius: calc(var(--cw) * 0.09);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  box-sizing: border-box;
}

.front {
  background: var(--paper);
  box-shadow: inset 0 0 0 1.5px #d6ccb4, 0 calc(var(--cw) * 0.05) 0 rgba(0, 0, 0, 0.35);
  overflow: hidden;
}

.front .w {
  position: absolute;
  left: 8%;
  top: 2%;
  font: 700 calc(var(--cw) * 0.38) / 1.05 var(--read);
  color: var(--ink);
  letter-spacing: 0.01em;
}

.front .w.rev {
  left: auto;
  top: auto;
  right: 8%;
  bottom: 2%;
  transform: rotate(180deg);
}

.dia {
  display: inline-block;
  width: 0.7em;
  height: 0.7em;
  background: var(--lv);
  transform: rotate(45deg);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.3);
}

.front .dia {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 17%;
  height: auto;
  aspect-ratio: 1;
  transform: translate(-50%, -50%) rotate(45deg);
}

.lv-vert { --lv: #2f9e44; }
.lv-jaune { --lv: #f5c518; }
.lv-rouge { --lv: #e03131; }

.back {
  transform: rotateY(180deg);
  background-color: #c8423a;
  background-image:
    repeating-linear-gradient(45deg, rgba(255, 255, 255, 0.13) 0 3px, transparent 3px 9px),
    repeating-linear-gradient(-45deg, rgba(0, 0, 0, 0.13) 0 3px, transparent 3px 9px);
  border: calc(var(--cw) * 0.07) solid #f2e8d2;
  box-shadow: 0 calc(var(--cw) * 0.05) 0 rgba(0, 0, 0, 0.35);
}

.back i {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 26%;
  aspect-ratio: 1;
  transform: translate(-50%, -50%) rotate(45deg);
  border: calc(var(--cw) * 0.04) solid #f2e8d2;
  background: #a8322b;
}

.card.sel .front {
  box-shadow: inset 0 0 0 1.5px #d6ccb4, 0 0 0 3px #ffd43b, 0 calc(var(--cw) * 0.12) 0 rgba(0, 0, 0, 0.3);
}

.card.lift .front, .card.lift .back {
  box-shadow: inset 0 0 0 1.5px #d6ccb4, 0 calc(var(--cw) * 0.2) calc(var(--cw) * 0.1) rgba(0, 0, 0, 0.35);
}

.card.hint .front::after, .card.target .front::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  border: 3px solid #4dabf7;
  animation: pulse 0.6s ease-in-out infinite alternate;
}

.card.target .front::after { border-color: #69db7c; border-width: 4px; animation: none; }

@keyframes pulse { from { opacity: 0.35; } to { opacity: 1; } }

/* ------------------------------------------------------------- labels */

.lbl {
  position: absolute;
  transform: translateX(-50%);
  font: 500 12px/1.2 var(--ui);
  color: #dcefe4;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.5);
  white-space: nowrap;
}

.lbl.slot {
  border: 2px dashed rgba(255, 255, 255, 0.16);
  border-radius: 8px;
  box-sizing: border-box;
}

.lbl.name {
  padding: 2px 8px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.35);
  font-size: 13px;
}

.lbl.name.turn { background: var(--pc); color: #fff; animation: bob 0.9s ease-in-out infinite alternate; }

.lbl.tag {
  padding: 0 5px;
  border-radius: 5px;
  background: var(--pc);
  color: #fff;
  font-size: 11px;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.4);
}

.lbl.hint-text { font: 14px var(--read); opacity: 0.55; }

.p0 { --pc: var(--p0); }
.p1 { --pc: var(--p1); }
.p2 { --pc: var(--p2); }
.p3 { --pc: var(--p3); }

@keyframes bob { from { transform: translateX(-50%) translateY(0); } to { transform: translateX(-50%) translateY(-3px); } }

/* ------------------------------------------------------------- bars */

#hud {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 6px) var(--sr) 6px var(--sl);
}

#hud .status { flex: 1; text-align: center; font-size: 17px; text-shadow: 0 2px 0 rgba(0, 0, 0, 0.5); }

.chip {
  padding: 5px 10px;
  border-radius: 8px;
  background: var(--panel);
  border: 2px solid var(--panel-edge);
  font-size: 14px;
}

#bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 5;
  padding: 2px var(--sr) calc(env(safe-area-inset-bottom) + 8px) var(--sl);
  /* Ends in the page colour, which browsers also paint below the viewport. */
  background: linear-gradient(rgba(13, 38, 34, 0), rgba(13, 38, 34, 0.85) 40%, #0d2622);
}

body:not(.in-game) #hud, body:not(.in-game) #bar { display: none; }

.tip {
  font: 14px/1.3 var(--read);
  color: #e6f2ea;
  text-align: center;
  padding: 2px 4px 6px;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.6);
}

.buttons { display: flex; gap: 8px; justify-content: center; min-height: 44px; }
.buttons .btn { flex: 1; max-width: 210px; }

/* ------------------------------------------------------------- buttons */

.btn {
  --c: #4f6367;
  --d: #2b3739;
  font: 700 16px var(--ui);
  color: #fff;
  border: 0;
  border-radius: 10px;
  padding: 10px 14px;
  background: var(--c);
  box-shadow: 0 4px 0 var(--d);
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.3);
  cursor: pointer;
  transition: transform 0.07s, box-shadow 0.07s, filter 0.15s;
  touch-action: manipulation;
}

.btn:hover { filter: brightness(1.1); }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--d); }
.btn:disabled { filter: grayscale(1) brightness(0.65); cursor: default; transform: none; }
.btn.small { padding: 6px 10px; font-size: 14px; }
.btn.off { opacity: 0.5; text-decoration: line-through; }
.btn.big { font-size: 22px; padding: 13px 18px; }
.btn.green { --c: #4bc292; --d: #2b7d5c; }
.btn.red { --c: #fe5f55; --d: #a3332c; }
.btn.blue { --c: #009dff; --d: #00609e; }
.btn.orange { --c: #fda200; --d: #a86b00; }
.btn.dark { --c: #4f6367; --d: #2b3739; }

/* ------------------------------------------------------------- toast */

#toast {
  position: fixed;
  left: 50%;
  top: 40%;
  transform: translateX(-50%);
  z-index: 6;
  pointer-events: none;
  width: max-content;
  max-width: calc(100vw - 24px);
}

.msg {
  background: rgba(22, 34, 36, 0.94);
  border: 2px solid var(--panel-edge);
  color: #fff;
  padding: 8px 14px;
  border-radius: 12px;
  font: 16px/1.3 var(--read);
  text-align: center;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.35);
  animation: pop 0.3s cubic-bezier(0.2, 1.7, 0.4, 1);
}

.msg.err { background: #c92a2a; border-color: #7d1a1a; animation: pop 0.3s cubic-bezier(0.2, 1.7, 0.4, 1), shake 0.35s; }
.msg b, .panel b { font-family: var(--read); font-weight: 700; color: #ffe066; }

@keyframes pop { from { transform: scale(0.6); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes shake {
  20% { translate: -6px 0; }
  40% { translate: 6px 0; }
  60% { translate: -4px 0; }
  80% { translate: 3px 0; }
}

/* ------------------------------------------------------------- overlays */

#overlay { position: fixed; inset: 0; z-index: 10; pointer-events: none; }

.backdrop {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(6, 14, 16, 0.62);
  pointer-events: auto;
  padding: 16px max(16px, env(safe-area-inset-right)) 16px max(16px, env(safe-area-inset-left));
  box-sizing: border-box;
}

.backdrop.curtain { background: rgba(6, 14, 16, 0.94); }

.panel {
  background: var(--panel);
  border: 3px solid var(--panel-edge);
  border-radius: 16px;
  box-shadow: 0 6px 0 rgba(0, 0, 0, 0.45), inset 0 0 0 2px rgba(255, 255, 255, 0.06);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  max-width: 420px;
  box-sizing: border-box;
  pointer-events: auto;
}

.panel.pop { animation: pop 0.35s cubic-bezier(0.2, 1.6, 0.4, 1); }
.panel h2 { margin: 0; text-align: center; font-size: 26px; text-shadow: 0 3px 0 rgba(0, 0, 0, 0.4); }
.panel h2 span { color: var(--pc); }
.panel h3 { margin: 8px 0 0; font-size: 18px; color: #ffd43b; }
.panel p { margin: 0; font: 15px/1.4 var(--read); color: #e6f2ea; text-align: center; }
.panel.rules { max-height: 88dvh; overflow-y: auto; touch-action: pan-y; }
.backdrop .panel { max-height: 100%; overflow-y: auto; touch-action: pan-y; }
.panel.rules p { text-align: left; }
.panel .ex { text-align: center; }
.panel .row { display: flex; gap: 8px; }
.panel .row .btn { flex: 1; padding-left: 6px; padding-right: 6px; }
.big-num { text-align: center; font-size: 44px; color: #ffd43b; text-shadow: 0 4px 0 rgba(0, 0, 0, 0.4); }

table { width: 100%; border-collapse: collapse; font-size: 17px; }
th { font-size: 13px; opacity: 0.7; font-weight: 500; }
td, th { padding: 5px 6px; text-align: center; }
td:first-child { text-align: left; color: var(--pc); }
tr.lead td:last-child { color: #ffd43b; }
tr + tr { border-top: 1px solid rgba(255, 255, 255, 0.08); }

/* ------------------------------------------------------------- menu */

.menu {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: calc(env(safe-area-inset-top) + 12px) max(16px, env(safe-area-inset-right)) calc(env(safe-area-inset-bottom) + 12px) max(16px, env(safe-area-inset-left));
  box-sizing: border-box;
  pointer-events: none;
}

.title {
  margin: 0;
  display: flex;
  align-items: baseline;
  gap: 0.25em;
  font: 700 clamp(44px, 13vw, 72px) / 1 var(--ui);
  letter-spacing: 0.04em;
  animation: sway 3.2s ease-in-out infinite;
}

.title span {
  color: #fff;
  text-shadow: 0 5px 0 #a3332c, 0 9px 0 rgba(0, 0, 0, 0.35);
}

.title span:last-child { color: #ffe066; text-shadow: 0 5px 0 #a86b00, 0 9px 0 rgba(0, 0, 0, 0.35); }
.title small { font-size: 0.42em; color: #dcefe4; text-shadow: 0 3px 0 rgba(0, 0, 0, 0.4); }

@keyframes sway {
  0%, 100% { transform: rotate(-2deg) translateY(0); }
  50% { transform: rotate(2deg) translateY(-4px); }
}

#menu-cards { width: min(420px, 100%); height: clamp(96px, 19dvh, 150px); flex-shrink: 0; }

.segs { display: flex; gap: 6px; }

.seg, .lvl {
  flex: 1;
  font: 700 15px var(--ui);
  color: #fff;
  border: 0;
  border-radius: 9px;
  padding: 9px 6px;
  background: #2b3739;
  box-shadow: 0 3px 0 #192123;
  cursor: pointer;
  transition: transform 0.07s;
  touch-action: manipulation;
}

.seg:active, .lvl:active { transform: translateY(2px); }
.seg.on { background: #fe5f55; box-shadow: 0 3px 0 #a3332c; }

.lvl { display: flex; align-items: center; justify-content: center; gap: 8px; text-transform: capitalize; opacity: 0.55; }
.lvl .dia { width: 0.75em; height: 0.75em; }
.lvl.on { opacity: 1; background: #46595d; box-shadow: 0 3px 0 #192123, inset 0 0 0 2px var(--lv); }

/* ------------------------------------------------------------- loader */

#loader {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: #0d2622;
}

#loader p { font: 16px var(--read); opacity: 0.7; }

/* ------------------------------------------------------------- phone on its side */

@media (orientation: landscape) and (max-height: 520px) {
  /* The hint and the buttons share one row, leaving the table more height. */
  #bar { display: flex; align-items: center; gap: 12px; padding-top: 6px; }
  #bar .tip { flex: 1; text-align: left; padding: 0 4px; }
  #bar .buttons { flex: 0 1 auto; min-height: 40px; }
  #bar .buttons .btn { flex: 0 0 auto; min-width: 110px; white-space: nowrap; padding-top: 8px; padding-bottom: 8px; }

  /* Menu: title and cards on the left, the panel on the right. */
  .menu {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(280px, 380px);
    grid-template-rows: auto auto;
    align-content: center;
    justify-content: center;
    column-gap: 20px;
    row-gap: 4px;
  }
  .menu .title { grid-column: 1; grid-row: 1; justify-self: center; font-size: clamp(32px, min(13dvh, 6.5vw), 64px); }
  .menu #menu-cards { grid-column: 1; grid-row: 2; justify-self: center; height: clamp(80px, 32dvh, 140px); }
  .menu .panel { grid-column: 2; grid-row: 1 / span 2; align-self: center; gap: 8px; padding: 12px; max-height: 100%; overflow-y: auto; touch-action: pan-y; }
}
