/* Kalamun on the web, set like a newspaper's puzzle page: newsprint, black
   ink and printed rules, square corners, a Naskh headline face. The player's
   answers are written in the app's blue pen (Kalamun/Design/Theme.swift), and
   the yellow highlighter marks where they are. Phone first: each game fills
   the screen, with its keyboard at the bottom. */
@font-face { font-family: "Aref Ruqaa"; src: url("/fonts/aref-ruqaa-700.woff2") format("woff2"); font-weight: 700; font-display: swap; }
@font-face { font-family: "Noto Naskh Arabic"; src: url("/fonts/naskh-arabic-500.woff2") format("woff2"); font-weight: 400 500; font-display: swap; }
@font-face { font-family: "Noto Naskh Arabic"; src: url("/fonts/naskh-arabic-700.woff2") format("woff2"); font-weight: 600 700; font-display: swap; }
@font-face { font-family: "IBM Plex Sans Arabic"; src: url("/fonts/plex-arabic-400.woff2") format("woff2"); font-weight: 400; font-display: swap; }
@font-face { font-family: "IBM Plex Sans Arabic"; src: url("/fonts/plex-arabic-600.woff2") format("woff2"); font-weight: 600; font-display: swap; }

:root {
  --paper: #F6F2E8;        /* newsprint */
  --sheet: #FFFDF7;        /* the puzzle squares, a shade brighter */
  --ink: #141414;
  --muted: #5E5A50;
  --hair: #D6D0C2;         /* hairline rules */
  --tray: #E9E4D7;
  --pencil: #CFC9BA;
  --pen: #2337D8;
  --on-pen: #FFFFFF;
  --red: #C2321F;
  --highlight: #FFE14D;
  --highlight-tint: #FBEFB0;
  --mint: #A8E0C4;
  --lavender: #C9BEF5;
  --coral: #F7A08B;
  --hand: "Aref Ruqaa", serif;
  --print: "Noto Naskh Arabic", "Geeza Pro", serif;
  --ui: "IBM Plex Sans Arabic", system-ui, sans-serif;
  color-scheme: light;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 16px/1.65 var(--ui);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
button, a { font: inherit; color: inherit; touch-action: manipulation; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--pen); outline-offset: 2px; }
h1, h2, h3, p { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 0.86rem; }
.hand { font-family: var(--hand); font-weight: 700; color: var(--pen); }
.hand.big { font-size: 2.8rem; line-height: 1.35; }
.display { font: 700 1.7rem/1.35 var(--print); }
[hidden] { display: none !important; }

/* A newspaper's double rule: thick over thin. */
.rule2 { border: 0; height: 5px; margin: 0; border-top: 2.5px solid var(--ink); border-bottom: 1px solid var(--ink); }

.app {
  min-height: 100dvh;
  max-width: 36rem;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
}
/* Game screens are exactly one screen tall: the grid shrinks, the keyboard stays. */
.app.crossword, .app.wordguess { height: 100dvh; overflow: hidden; }
.app.crossword { max-width: 72rem; }

.loading { flex: 1; display: grid; place-content: center; justify-items: center; gap: 0.75rem; text-align: center; padding: 2rem; }
.loading-mark { font: 700 3.5rem/1.2 var(--hand); color: var(--ink); }
.noscript { text-align: center; padding: 2rem; }

/* Buttons: printed boxes, no pills. */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 2.9rem;
  padding: 0 1.4rem;
  border: 1.5px solid var(--ink);
  border-radius: 2px;
  background: var(--ink);
  color: var(--paper);
  font-weight: 600;
  text-decoration: none;
}
.btn:active { transform: translateY(1px); }
.btn.ghost { background: transparent; color: var(--ink); }
.btn.ghost:hover { background: var(--tray); }
.btn:disabled { opacity: 0.3; cursor: default; transform: none; }
.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 2.6rem;
  height: 2.6rem;
  border: 0;
  border-radius: 0;
  background: transparent;
  font-size: 1.2rem;
  text-decoration: none;
  flex: none;
}
.icon-btn:hover { background: var(--tray); }
.row { display: flex; flex-wrap: wrap; gap: 0.6rem; justify-content: center; }

/* A game's top bar: back, title, tools, over a double rule. */
.topbar {
  display: flex;
  align-items: center;
  gap: 0.15rem;
  padding: 0.3rem 0.4rem 0.35rem;
  flex: none;
  border-bottom: 1px solid var(--ink);
  box-shadow: 0 2px 0 var(--paper), 0 3.5px 0 var(--ink);
  margin-bottom: 4px;
  background: var(--paper);
}
.topbar-title { flex: 1; text-align: center; min-width: 0; }
.topbar h1 { font: 700 1.2rem/1.25 var(--print); }
.topbar .sub { font-size: 0.74rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-actions { display: flex; align-items: center; }
.timer { font-size: 0.86rem; font-weight: 600; min-width: 2.6rem; text-align: center; font-variant-numeric: tabular-nums; }
.level-btn {
  border: 1px solid var(--ink);
  border-radius: 2px;
  background: transparent;
  padding: 0 0.55rem;
  font-size: 0.78rem;
  font-weight: 600;
  min-height: 1.8rem;
  margin-inline-end: 0.2rem;
  white-space: nowrap;
}
.level-btn:hover { background: var(--tray); }

/* ─── Home: the puzzle page ─────────────────────────────────────────────── */
.masthead { padding: 0.9rem 1rem 0; text-align: center; }
.mast-ears {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  font-size: 0.74rem;
  color: var(--muted);
  min-height: 1.2rem;
}
.mast-ears .streak { color: var(--ink); font-weight: 600; background: var(--highlight); padding: 0 0.35rem; }
.wordmark { display: inline-block; font: 700 3.3rem/1.1 var(--hand); color: var(--ink); text-decoration: none; padding: 0.15rem 0 0.35rem; }
.dateline {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.3rem 0.1rem;
  font-size: 0.8rem;
  border-bottom: 1px solid var(--ink);
}
.dateline b { font-weight: 600; }

.home-main { padding: 0 1rem calc(2rem + env(safe-area-inset-bottom)); }
.kicker {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin: 1.6rem 0 0.2rem;
  font: 700 1.05rem/1.4 var(--print);
}
.all-done { margin: 1.4rem 0 0.4rem; padding: 0.9rem 0; text-align: center; border-bottom: 1px solid var(--hair); }
.all-done .hand { font-size: 2.6rem; line-height: 1.3; }

/* Today's puzzles, as a page index: number, title, standfirst, action. */
.cards { display: grid; border-top: 2.5px solid var(--ink); }
.card {
  display: grid;
  grid-template-columns: 2.6rem 1fr auto;
  align-items: center;
  gap: 0.9rem;
  padding: 0.95rem 0.1rem;
  border-bottom: 1px solid var(--ink);
  text-decoration: none;
}
.card:hover { background: rgba(20, 20, 20, 0.035); }
.card-num { font: 700 2.3rem/1 var(--print); text-align: center; color: var(--ink); }
.card-body { display: grid; min-width: 0; }
.card-title { font: 700 1.45rem/1.3 var(--print); }
.card-line { color: var(--muted); font-size: 0.88rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-go {
  padding: 0.35rem 0.85rem;
  border: 1.5px solid var(--ink);
  border-radius: 2px;
  background: var(--ink);
  color: var(--paper);
  font-weight: 600;
  font-size: 0.86rem;
}
.card-go::after { content: " ←"; }
.card.done .card-go { background: transparent; color: var(--ink); border-color: transparent; padding-inline: 0.2rem; }
.card.done .card-go::after { content: ""; }
.card.done .card-title { text-decoration: line-through; text-decoration-thickness: 1.5px; text-decoration-color: var(--pen); }

/* Settings: a boxed panel, like a weather box. */
.settings { margin-top: 1.6rem; border: 1px solid var(--ink); }
.settings-head { padding: 0.25rem 0.75rem; background: var(--ink); color: var(--paper); font: 700 0.92rem/1.6 var(--print); }
.setting { display: flex; align-items: center; justify-content: space-between; gap: 0.6rem; flex-wrap: wrap; padding: 0.6rem 0.75rem; border-bottom: 1px solid var(--hair); }
.setting-label { font-weight: 600; font-size: 0.92rem; }
.setting-note { margin: 0; padding: 0.5rem 0.75rem; font-size: 0.8rem; color: var(--muted); }
.seg { display: inline-flex; border: 1.5px solid var(--ink); border-radius: 2px; }
.seg button {
  border: 0;
  background: transparent;
  padding: 0.25rem 0.85rem;
  font-size: 0.88rem;
  min-height: 2.2rem;
  border-radius: 0;
}
.seg button + button { border-inline-start: 1px solid var(--ink); }
.seg button.on { background: var(--ink); color: var(--paper); font-weight: 600; }
.seg button:disabled { opacity: 0.3; cursor: default; }
.seg.big { display: flex; margin: 0.5rem 0 1rem; }
.seg.big button { flex: 1; min-height: 2.75rem; font-size: 1rem; }

.archive ul { list-style: none; margin: 0; padding: 0; border-top: 2.5px solid var(--ink); }
.arch-day {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.65rem 0.1rem;
  border: 0;
  border-bottom: 1px solid var(--hair);
  background: transparent;
  text-align: start;
}
.arch-day:hover { background: rgba(20, 20, 20, 0.035); }
.dots { display: inline-flex; gap: 0.25rem; }
.dots i { width: 0.75rem; height: 0.75rem; border: 1.5px solid var(--ink); }
.dots i.on { background: var(--ink); }
.foot { margin-top: 2rem; padding-top: 0.6rem; border-top: 1px solid var(--ink); font-size: 0.8rem; text-align: center; }
.foot a { color: var(--muted); }

/* ─── Keyboard ──────────────────────────────────────────────────────────── */
.bottom { flex: none; padding-bottom: max(0.4rem, env(safe-area-inset-bottom)); }
.keyboard { display: grid; gap: 0.3rem; padding: 0.5rem 0.25rem 0; user-select: none; -webkit-user-select: none; }
.krow { display: flex; gap: 0.2rem; justify-content: center; }
.key {
  flex: 1 1 0;
  min-width: 0;
  max-width: 3.2rem;
  height: 3.2rem;
  border: 1px solid var(--pencil);
  border-bottom: 2px solid #B9B2A1;
  border-radius: 3px;
  background: var(--sheet);
  font: 700 1.3rem/1 var(--print);
  padding: 0;
}
.key:active { background: var(--tray); border-bottom-width: 1px; }
.key.wide { flex: 1.6 1 0; max-width: 4.6rem; font: 600 0.8rem/1 var(--ui); background: var(--tray); }
.key.m0 { background: #8E897D; border-color: #8E897D; color: #F6F2E8; }
.key.m1 { background: var(--highlight); border-color: #D9BC2E; }
.key.m2 { background: var(--pen); border-color: #18299F; color: var(--on-pen); }
@media (max-height: 640px) { .key { height: 2.7rem; } }

/* ─── Crossword ─────────────────────────────────────────────────────────── */
.cw-main { flex: 1; min-height: 0; display: flex; }
.cw-play { flex: 1; min-width: 0; display: flex; flex-direction: column; max-width: 34rem; margin: 0 auto; }
.cw-stage { flex: 1; min-height: 0; container-type: size; display: grid; place-items: center; padding: 0.6rem; }
.cw-grid {
  /* Fit both ways, leaving room for the 1px rules between cells and the 3px frame. */
  --cell: min((100cqw - var(--cols) * 1px - 6px) / var(--cols), (100cqh - var(--rows) * 1px - 6px) / var(--rows));
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell));
  grid-auto-rows: var(--cell);
  border: 3px solid var(--ink);
  background: var(--ink);
  gap: 1px;
}
.cw-cell { position: relative; border: 0; padding: 0; background: var(--sheet); display: grid; place-items: center; overflow: hidden; }
.cw-cell.block { background: var(--ink); }
.cw-cell.word { background: var(--highlight-tint); }
.cw-cell.sel { background: var(--highlight); }
.cw-cell .num { position: absolute; top: 1px; right: 3px; font: 600 calc(var(--cell) * 0.24)/1 var(--ui); color: var(--ink); }
.cw-cell .letter { font: 700 calc(var(--cell) * 0.5)/1 var(--hand); color: var(--pen); padding-top: calc(var(--cell) * 0.1); }
.cw-cell.wrong .letter { color: var(--red); }
.cw-cell.wrong::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(to top right, transparent 48%, var(--red) 49%, var(--red) 51%, transparent 52%);
  opacity: 0.6;
}
.cw-cell.revealed::before {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  border: calc(var(--cell) * 0.11) solid transparent;
  border-bottom-color: var(--red);
  border-left-color: var(--red);
}
.cw-cell.solved .letter { color: var(--ink); }

/* The current clue, printed between rules. */
.cluebar {
  display: flex;
  align-items: center;
  margin: 0 0.4rem;
  border-top: 2.5px solid var(--ink);
  border-bottom: 1px solid var(--ink);
  min-height: 3.6rem;
  background: var(--paper);
}
.clue-now { flex: 1; min-width: 0; border: 0; background: transparent; text-align: start; padding: 0.35rem 0.2rem; display: grid; line-height: 1.45; }
.clue-tag { font-size: 0.72rem; font-weight: 600; color: var(--muted); }
.clue-text { font: 500 1.05rem/1.5 var(--print); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.banner { margin: 0 0.4rem; text-align: center; font-size: 0.9rem; }
.banner:not(:empty) { padding: 0.35rem 0.75rem; margin-bottom: 0.4rem; border: 1px solid currentColor; }
.banner.warn { color: var(--red); background: #FBE6DF; }
.banner.good { background: var(--highlight-tint); }

.cw-lists { display: none; }
@media (min-width: 60rem) {
  .cw-lists {
    display: block;
    width: 23rem;
    flex: none;
    overflow-y: auto;
    padding: 0.5rem 1.1rem 2rem;
    border-inline-start: 1px solid var(--ink);
  }
  .cw-lists h2 { font: 700 1.05rem/1.5 var(--print); margin: 0.8rem 0 0.2rem; padding-bottom: 0.15rem; border-bottom: 2.5px solid var(--ink); }
  .cw-lists ol { list-style: none; margin: 0; padding: 0; }
  .clue-item {
    width: 100%;
    display: flex;
    gap: 0.6rem;
    text-align: start;
    border: 0;
    border-bottom: 1px solid var(--hair);
    background: transparent;
    padding: 0.35rem 0.3rem;
    font: 500 0.98rem/1.55 var(--print);
  }
  .clue-item b { min-width: 1.4rem; font-family: var(--ui); font-weight: 600; }
  .clue-item.filled { color: var(--muted); }
  .clue-item.on { background: var(--highlight-tint); color: var(--ink); }
  .clue-item:hover { background: var(--tray); }
}

.assist { display: grid; gap: 0; margin: 0.8rem 0; border-top: 2.5px solid var(--ink); }
.assist-row { display: grid; grid-template-columns: 4rem 1fr 1fr; gap: 0.5rem; align-items: center; font-weight: 600; padding: 0.5rem 0; border-bottom: 1px solid var(--hair); }
.assist-row .btn { min-height: 2.5rem; padding: 0 0.5rem; }

/* ─── «خمّن» ────────────────────────────────────────────────────────────── */
.wg-main { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.wg-stage { flex: 1; min-height: 0; container-type: size; display: grid; place-items: center; padding: 0.75rem 1.5rem 0.25rem; }
.wg-board { --tile: min(100cqw / 5.4, 100cqh / (var(--rows) * 1.08), 4.2rem); display: grid; gap: calc(var(--tile) * 0.08); }
.wg-row { display: grid; grid-template-columns: repeat(5, var(--tile)); gap: calc(var(--tile) * 0.08); }
.wg-tile {
  width: var(--tile);
  height: var(--tile);
  display: grid;
  place-items: center;
  border: 1.5px solid var(--pencil);
  background: var(--sheet);
  font: 700 calc(var(--tile) * 0.5)/1 var(--hand);
  color: var(--pen);
  padding-top: calc(var(--tile) * 0.1);
}
.wg-tile.typed { border: 2px solid var(--ink); animation: pop 0.1s; }
.wg-tile.m0 { background: #8E897D; border-color: #8E897D; color: #F6F2E8; }
.wg-tile.m1 { background: var(--highlight); border-color: var(--highlight); color: var(--ink); }
.wg-tile.m2 { background: var(--pen); border-color: var(--pen); color: var(--on-pen); }
.wg-row.flip .wg-tile { animation: flip 0.5s both; }
.wg-row.flip .wg-tile:nth-child(2) { animation-delay: 0.15s; }
.wg-row.flip .wg-tile:nth-child(3) { animation-delay: 0.3s; }
.wg-row.flip .wg-tile:nth-child(4) { animation-delay: 0.45s; }
.wg-row.flip .wg-tile:nth-child(5) { animation-delay: 0.6s; }
.wg-msg { min-height: 1.6rem; text-align: center; color: var(--red); font-weight: 600; font-size: 0.92rem; flex: none; }
.wg-msg.good { color: var(--ink); }
.wordguess .bottom { padding-inline: 0.25rem; }
.legend { list-style: none; padding: 0; margin: 0.5rem 0 1rem; display: grid; gap: 0.4rem; }
.legend i { display: inline-block; width: 1.1rem; height: 1.1rem; vertical-align: -0.2rem; margin-inline-end: 0.5rem; }
.legend .m0 { background: #8E897D; }
.legend .m1 { background: var(--highlight); }
.legend .m2 { background: var(--pen); }

/* ─── «روابط» ───────────────────────────────────────────────────────────── */
.cn-main { padding: 0.9rem 0.75rem calc(1.5rem + env(safe-area-inset-bottom)); display: grid; gap: 0.45rem; align-content: start; }
.cn-groups { display: grid; gap: 0.45rem; }
.cn-groups:empty { display: none; }
.cn-group {
  padding: 0.55rem 0.8rem;
  text-align: center;
  display: grid;
  line-height: 1.5;
  color: var(--ink);
  border: 1.5px solid var(--ink);
  animation: land 0.35s ease-out;
}
.cn-group b { font: 700 1.05rem/1.5 var(--print); }
.cn-group span { font-size: 0.9rem; }
.cn-group.g1 { background: var(--highlight); }
.cn-group.g2 { background: var(--mint); }
.cn-group.g3 { background: var(--lavender); }
.cn-group.g4 { background: var(--coral); }
.cn-group.missed { border-style: dashed; opacity: 0.8; }
.cn-board { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0.4rem; }
.cn-tile {
  aspect-ratio: 1.15;
  min-width: 0;
  border: 1px solid var(--ink);
  border-radius: 2px;
  background: var(--sheet);
  font: 700 clamp(0.95rem, 4.6vw, 1.3rem)/1.3 var(--print);
  padding: 0.2rem;
  overflow-wrap: anywhere;
  transition: background 0.1s, color 0.1s;
}
.cn-tile.long { font-size: clamp(0.8rem, 3.8vw, 1.1rem); }
.cn-tile:active { transform: translateY(1px); }
.cn-tile.on { background: var(--ink); color: var(--paper); }
.cn-msg { min-height: 1.6rem; text-align: center; font-weight: 600; color: var(--red); }
.cn-mistakes { display: flex; justify-content: center; align-items: center; gap: 0.6rem; color: var(--muted); font-size: 0.9rem; }
.pips { display: inline-flex; gap: 0.3rem; }
.pips i { width: 0.75rem; height: 0.75rem; border: 1.5px solid var(--ink); }
.pips i.on { background: var(--ink); }
.cn-actions { display: flex; gap: 0.5rem; justify-content: center; margin-top: 0.4rem; }
.cn-actions .btn { padding: 0 1rem; flex: 1; max-width: 9rem; white-space: nowrap; }

/* Results. */
.result { text-align: center; display: grid; gap: 0.45rem; justify-items: center; padding: 1rem; }
.result .note { color: var(--muted); max-width: 26rem; font: 500 0.98rem/1.6 var(--print); padding-top: 0.5rem; border-top: 1px solid var(--hair); }
.share-preview { margin: 0.25rem 0; font: 1.2rem/1.25 system-ui; letter-spacing: 0.1em; direction: ltr; }
.result .row { margin-top: 0.4rem; }

/* Sheets: a slip of paper with a rule along the top. */
.sheet {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(20, 20, 20, 0.4);
  animation: fade 0.2s;
}
.sheet-card {
  position: relative;
  width: 100%;
  max-width: 32rem;
  max-height: 88dvh;
  overflow-y: auto;
  background: var(--paper);
  border-top: 4px solid var(--ink);
  padding: 1.25rem 1.2rem calc(1.5rem + env(safe-area-inset-bottom));
  animation: rise 0.25s ease-out;
}
@media (min-width: 40rem) {
  .sheet { align-items: center; }
  .sheet-card { border: 1.5px solid var(--ink); border-top-width: 4px; }
}
.sheet-close { position: absolute; top: 0.5rem; left: 0.5rem; font-size: 1rem; }
.sheet h2 { font: 700 1.4rem/1.4 var(--print); margin-bottom: 0.75rem; padding: 0 0 0.3rem; padding-inline-end: 2.5rem; border-bottom: 1px solid var(--ink); }
.sheet p { margin-bottom: 0.6rem; }
.sheet .cards { margin-top: 0.25rem; }
.celebrate { text-align: center; display: grid; gap: 0.3rem; justify-items: center; }
.celebrate .time { font: 700 2.6rem/1.2 var(--print); }
.celebrate .btn { width: 100%; max-width: 18rem; margin-top: 0.4rem; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(6rem + env(safe-area-inset-bottom));
  transform: translate(-50%, 1rem);
  background: var(--ink);
  color: var(--paper);
  padding: 0.5rem 1.1rem;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 30;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.shake { animation: shake 0.4s; }
@keyframes shake { 20%, 60% { transform: translateX(-5px); } 40%, 80% { transform: translateX(5px); } }
@keyframes flip { 0% { transform: rotateX(90deg); } 100% { transform: rotateX(0); } }
@keyframes pop { 50% { transform: scale(1.06); } }
@keyframes land { from { transform: scale(0.97); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(2rem); opacity: 0; } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }
