/* Cryptle - palette inspired by the rouge et jaune of the Ecole polytechnique,
   laid over the anthracite of the grand uniforme. Dark by default.

   Red means "right letter, right place". That is deliberate: red and yellow
   are the pair here, and they stay far apart in both hue and lightness, so the
   three states remain distinguishable for red-green colour blindness - which
   the usual green/yellow pairing does not manage. */

:root {
  --bg: #0f1319;
  --fg: #eceae6;
  --muted: #8d919c;
  --line: #2a303c;
  --tile: #161b23;
  --correct: #c8102e;   /* rouge */
  --present: #f2b705;   /* jaune */
  --absent: #3b4150;    /* anthracite */
  --on-correct: #ffffff;
  --on-present: #14171f;
  --on-absent: #eceae6;
  --key: #2a303c;
  --key-fg: #eceae6;
  --accent: #c8102e;
  --on-accent: #ffffff;
  --shadow: 0 10px 40px rgba(0, 0, 0, .5);
}

:root[data-theme="light"] {
  --bg: #f5f2ec;
  --fg: #14171f;
  --muted: #6b6f7b;
  --line: #ddd8ce;
  --tile: #ffffff;
  --correct: #b00d26;
  --present: #e0a400;
  --absent: #a0a4ae;
  --on-correct: #ffffff;
  --on-present: #14171f;
  --on-absent: #ffffff;
  --key: #e3ded4;
  --key-fg: #14171f;
  --accent: #b00d26;
  --on-accent: #ffffff;
  --shadow: 0 10px 40px rgba(20, 23, 31, .14);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--fg);
  font-family: ui-sans-serif, system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
}

/* ------------------------------------------------------------------ topbar */

.topbar {
  position: relative;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: .5rem;
  padding: .5rem .75rem .6rem;
}

/* half red, half yellow: the colours of the school, as a rule under the title */
.topbar::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 3px;
  background: linear-gradient(90deg, var(--correct) 0 50%, var(--present) 50% 100%);
}

.topbar h1 {
  margin: 0;
  font-size: clamp(1.25rem, 5vw, 1.75rem);
  font-weight: 800;
  letter-spacing: .16em;
  text-transform: uppercase;
  text-align: center;
  white-space: nowrap;
}

.topbar .num {
  font-size: .55em;
  font-weight: 600;
  letter-spacing: .05em;
  color: var(--muted);
  margin-left: .4em;
}

.topbar-right {
  display: flex;
  justify-content: flex-end;
  gap: .25rem;
}

.icon {
  background: none;
  border: 1px solid var(--line);
  color: var(--fg);
  border-radius: 8px;
  width: 2rem;
  height: 2rem;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
}

.icon:hover { border-color: var(--accent); }

/* ------------------------------------------------------------------- board */

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
  padding: .5rem;
  min-height: 0;
}

.board {
  display: grid;
  gap: 5px;
  margin: auto 0;
  padding: .25rem;
  /* --cols and --rows are set from JS; tiles shrink to fit width AND height.
     The second declaration wins where svh is supported (mobile URL bars). */
  --tile-size: min(
    calc((min(100vw, 32rem) - 2rem - (var(--cols) - 1) * 5px) / var(--cols)),
    calc((100vh - 15rem - (var(--rows) - 1) * 5px) / var(--rows)),
    3.5rem
  );
  --tile-size: min(
    calc((min(100vw, 32rem) - 2rem - (var(--cols) - 1) * 5px) / var(--cols)),
    calc((100svh - 15rem - (var(--rows) - 1) * 5px) / var(--rows)),
    3.5rem
  );
  grid-template-columns: repeat(var(--cols), var(--tile-size));
}

.tile {
  width: var(--tile-size);
  height: var(--tile-size);
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--line);
  border-radius: 6px;
  background: var(--tile);
  font-size: calc(var(--tile-size) * .5);
  font-weight: 700;
  text-transform: uppercase;
  user-select: none;
  transition: transform .1s ease;
}

.tile.filled { border-color: var(--absent); }
.tile.pop { animation: pop .1s ease; }

.tile.correct { background: var(--correct); border-color: var(--correct); color: var(--on-correct); }
.tile.present { background: var(--present); border-color: var(--present); color: var(--on-present); }
.tile.absent  { background: var(--absent);  border-color: var(--absent);  color: var(--on-absent); }

.tile.reveal { animation: flip .5s ease forwards; }

.row-shake { animation: shake .5s ease; }
.tile.win { animation: bounce .5s ease; }

@keyframes pop   { 50% { transform: scale(1.08); } }
@keyframes flip  { 0% { transform: rotateX(0); } 50% { transform: rotateX(90deg); } 100% { transform: rotateX(0); } }
@keyframes shake { 10%, 90% { transform: translateX(-2px); } 20%, 80% { transform: translateX(4px); } 30%, 50%, 70% { transform: translateX(-6px); } 40%, 60% { transform: translateX(6px); } }
@keyframes bounce{ 0%,100% { transform: translateY(0); } 40% { transform: translateY(-14px); } 70% { transform: translateY(-6px); } }

@media (prefers-reduced-motion: reduce) {
  .tile, .tile.reveal, .row-shake, .row-win .tile { animation: none !important; transition: none; }
}

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

.hint {
  margin: 0;
  padding: .4rem .8rem;
  border: 1px dashed var(--line);
  border-radius: 999px;
  color: var(--muted);
  font-size: .9rem;
  text-align: center;
  max-width: 32rem;
}

.toast {
  position: fixed;
  top: 4rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--fg);
  color: var(--bg);
  padding: .5rem .9rem;
  border-radius: 8px;
  font-weight: 600;
  font-size: .9rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity .15s ease;
  z-index: 20;
}

.toast.show { opacity: 1; }

/* ---------------------------------------------------------------- keyboard */

.keyboard {
  width: 100%;
  max-width: 34rem;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.krow {
  display: flex;
  justify-content: center;
  gap: 4px;
}

.key {
  flex: 1 1 auto;
  min-width: 0;
  height: 3.25rem;
  border: none;
  border-radius: 6px;
  background: var(--key);
  color: var(--key-fg);
  font-size: .9rem;
  font-weight: 700;
  text-transform: uppercase;
  cursor: pointer;
}

.key.wide { flex: 1.6 1 auto; font-size: .75rem; }
.key.correct { background: var(--correct); color: var(--on-correct); }
.key.present { background: var(--present); color: var(--on-present); }
.key.absent  { background: var(--absent);  color: var(--on-absent); opacity: .75; }
.key:active { transform: translateY(1px); }

/* ------------------------------------------------------------------ modals */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 30;
}

.modal[hidden] { display: none; }

.modal-card {
  position: relative;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 1.5rem;
  width: min(30rem, 100%);
  max-height: 85vh;
  overflow-y: auto;
}

.modal-card h2 { margin: 0 0 .75rem; font-size: 1.15rem; }
.modal-card p  { line-height: 1.5; }
.modal-card ul { line-height: 1.6; padding-left: 1.1rem; }

.close {
  position: absolute;
  top: .5rem;
  right: .6rem;
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1.5rem;
  cursor: pointer;
}

.sw { padding: 0 .35em; border-radius: 4px; }
.sw.correct { background: var(--correct); color: var(--on-correct); }
.sw.present { background: var(--present); color: var(--on-present); }
.sw.absent  { background: var(--absent);  color: var(--on-absent); }

.answer {
  font-size: 1.6rem;
  font-weight: 800;
  letter-spacing: .1em;
  text-align: center;
  margin: .25rem 0 .5rem;
}

.definition {
  color: var(--muted);
  font-size: .95rem;
  text-align: center;
  margin-bottom: 1rem;
}

.stats {
  display: flex;
  justify-content: center;
  gap: 1.25rem;
  margin: 1rem 0;
}

.stat { text-align: center; }
.stat b { display: block; font-size: 1.5rem; }
.stat span { color: var(--muted); font-size: .7rem; text-transform: uppercase; letter-spacing: .05em; }

.dist { display: flex; flex-direction: column; gap: 3px; margin-bottom: 1rem; }
.dist-row { display: flex; align-items: center; gap: .5rem; font-size: .8rem; }
.dist-row i { font-style: normal; color: var(--muted); width: 1rem; }
.dist-bar {
  background: var(--absent);
  color: var(--on-absent);
  padding: .1rem .4rem;
  border-radius: 3px;
  min-width: 1.5rem;
  text-align: right;
}
.dist-bar.best { background: var(--correct); color: var(--on-correct); }

.actions { display: flex; gap: .5rem; align-items: center; justify-content: center; }

.btn {
  background: var(--accent);
  color: var(--on-accent);
  border: none;
  border-radius: 8px;
  padding: .7rem 1.2rem;
  font-weight: 700;
  font-size: .95rem;
  cursor: pointer;
}

.countdown { text-align: center; color: var(--muted); font-size: .85rem; margin-top: .75rem; }
.countdown b { font-variant-numeric: tabular-nums; color: var(--fg); }
