:root{
  --paper:#fbfaf3;
  --paper-edge:#f1efe4;
  --grid:#d3ddec;
  --grid-strong:#bcd0e6;
  --ink:#1b3a6b;          /* ballpoint blue */
  --ink-soft:#42577d;
  --margin:#d8503f;       /* copybook red margin */
  --graphite:#33312c;
  --green:#2f7d4f;
  --amber:#b9842b;
  --muted:#8d897c;
  --card:#ffffff;
  --radius:14px;
  --shadow:0 14px 40px -18px rgba(27,58,107,.45);
  --maxw:760px;
}

*{box-sizing:border-box;}
html,body{margin:0;padding:0;}
body{
  font-family:"Tajawal",system-ui,-apple-system,Segoe UI,Roboto,sans-serif;
  color:var(--graphite);
  background:var(--paper);
  min-height:100dvh;
  -webkit-text-size-adjust:100%;
}
#paper-bg{
  position:fixed; inset:0; z-index:-1;
  background-color:var(--paper);
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size:26px 26px;
  box-shadow:inset 0 0 140px -40px rgba(120,110,80,.35);
}
#app{
  max-width:var(--maxw);
  margin:0 auto;
  padding:clamp(16px,4vw,40px) clamp(14px,4vw,28px) 80px;
}

/* ---------- typography ---------- */
.brand{ text-align:center; margin:18px 0 26px; }
.brand .ar{
  font-family:"Aref Ruqaa",serif; font-weight:700;
  font-size:clamp(34px,9vw,58px); color:var(--ink); line-height:1;
  letter-spacing:.5px;
}
.brand .en{
  font-family:"Caveat",cursive; font-weight:700;
  font-size:clamp(22px,5vw,30px); color:var(--margin);
  margin-top:2px; transform:rotate(-1.4deg);
}
.brand .tag{ color:var(--muted); font-size:14px; margin-top:8px; }

h2.screen-title{
  font-family:"Aref Ruqaa",serif; color:var(--ink);
  font-size:clamp(22px,5vw,30px); margin:0 0 4px;
}
.subtle{ color:var(--muted); font-size:14px; margin:0 0 20px; }

/* ---------- card ---------- */
.card{
  background:var(--card);
  border:1px solid var(--paper-edge);
  border-radius:var(--radius);
  box-shadow:var(--shadow);
  padding:clamp(18px,4vw,28px);
  position:relative;
}
.card + .card{ margin-top:18px; }

/* ---------- forms ---------- */
label.field{ display:block; font-weight:500; font-size:14px; margin:14px 0 6px; color:var(--ink-soft); }
input[type=text], input[type=number], select{
  width:100%; font:inherit; font-size:16px; color:var(--graphite);
  padding:12px 14px; border:2px solid var(--grid-strong); border-radius:10px;
  background:#fff; outline:none; transition:border-color .15s, box-shadow .15s;
}
input:focus, select:focus{ border-color:var(--ink); box-shadow:0 0 0 3px rgba(27,58,107,.15); }
input::placeholder{ color:#b7b2a4; }

.btn{
  display:inline-flex; align-items:center; justify-content:center; gap:8px;
  font:inherit; font-weight:700; font-size:16px; cursor:pointer;
  padding:13px 20px; border-radius:11px; border:2px solid transparent;
  transition:transform .08s, box-shadow .15s, background .15s; width:100%;
}
.btn:active{ transform:translateY(1px); }
.btn-primary{ background:var(--ink); color:#fff; }
.btn-primary:hover{ box-shadow:0 8px 20px -8px rgba(27,58,107,.7); }
.btn-red{ background:var(--margin); color:#fff; }
.btn-red:hover{ box-shadow:0 8px 20px -8px rgba(216,80,63,.7); }
.btn-ghost{ background:#fff; color:var(--ink); border-color:var(--grid-strong); }
.btn-ghost:hover{ border-color:var(--ink); }
.btn:disabled{ opacity:.5; cursor:not-allowed; transform:none; }
.btn.small{ width:auto; padding:9px 14px; font-size:14px; }
.row{ display:flex; gap:12px; flex-wrap:wrap; }
.row > *{ flex:1 1 0; min-width:120px; }
.divider{ text-align:center; color:var(--muted); margin:18px 0; font-size:13px; letter-spacing:1px; }

/* ---------- lobby ---------- */
.codebox{
  display:flex; align-items:center; justify-content:space-between; gap:12px;
  background:var(--paper); border:2px dashed var(--grid-strong); border-radius:12px;
  padding:14px 16px; margin:6px 0 4px;
}
.codebox .code{ font-family:"Aref Ruqaa",serif; font-size:30px; letter-spacing:6px; color:var(--ink); }
.players{ list-style:none; padding:0; margin:14px 0 0; display:grid; gap:8px; }
.players li{
  display:flex; align-items:center; gap:10px; padding:10px 12px;
  background:var(--paper); border-radius:10px; border:1px solid var(--paper-edge);
}
.dot{ width:9px; height:9px; border-radius:50%; background:#cdc8ba; flex:0 0 auto; }
.dot.on{ background:var(--green); }
.tag-host{ font-size:11px; font-weight:700; color:var(--margin); border:1px solid var(--margin); border-radius:6px; padding:1px 6px; }
.tag-you{ font-size:11px; color:var(--muted); }

/* ---------- letter pick ---------- */
.letter-grid{ display:grid; grid-template-columns:repeat(auto-fill,minmax(48px,1fr)); gap:8px; margin-top:14px; }
.letter-grid button{
  aspect-ratio:1; font:inherit; font-family:"Aref Ruqaa",serif; font-size:22px;
  border:2px solid var(--grid-strong); border-radius:10px; background:#fff; color:var(--ink);
  cursor:pointer; transition:transform .08s, background .12s, border-color .12s;
}
.letter-grid button:hover:not(:disabled){ border-color:var(--ink); transform:translateY(-2px); }
.letter-grid button:disabled{ opacity:.3; cursor:not-allowed; background:var(--paper); text-decoration:line-through; }

/* big stamped letter */
.stamp{
  font-family:"Aref Ruqaa",serif; color:var(--margin);
  font-size:clamp(54px,16vw,96px); line-height:1; text-align:center;
  border:4px double var(--margin); border-radius:18px; width:fit-content; margin:6px auto 4px;
  padding:6px 28px; transform:rotate(-3deg);
}

/* ---------- the answer sheet (signature element) ---------- */
.sheet{ width:100%; border-collapse:collapse; margin-top:8px; font-size:15px; }
.sheet th, .sheet td{ border:1px solid var(--grid-strong); padding:0; }
.sheet thead th{
  background:var(--ink); color:#fff; font-family:"Tajawal"; font-weight:700;
  padding:10px 8px; font-size:14px;
}
.sheet td .catlabel{ display:none; }
.sheet input.cell{
  border:none; border-radius:0; width:100%; background:transparent;
  font-family:"Caveat","Tajawal",cursive; font-size:22px; color:var(--ink);
  padding:10px 12px;
}
html[dir=rtl] .sheet input.cell{ font-family:"Tajawal"; font-size:19px; }
.sheet input.cell:focus{ box-shadow:inset 0 0 0 2px rgba(27,58,107,.25); }
.sheet .lettercol{ background:var(--paper); text-align:center; font-family:"Aref Ruqaa",serif; color:var(--margin); font-size:24px; width:46px; }

/* timer / status bar */
.statusbar{ display:flex; align-items:center; justify-content:space-between; gap:12px; margin:2px 0 14px; flex-wrap:wrap; }
.timer{ font-family:"Aref Ruqaa",serif; font-size:30px; color:var(--ink); }
.timer.low{ color:var(--margin); animation:pulse .8s infinite; }
@keyframes pulse{ 50%{ opacity:.45; } }
.pill{ background:var(--paper); border:1px solid var(--paper-edge); border-radius:999px; padding:6px 14px; font-size:13px; color:var(--ink-soft); }

/* ---------- review ---------- */
.scroll-x{ overflow-x:auto; -webkit-overflow-scrolling:touch; }
.review-table{ border-collapse:collapse; width:100%; min-width:420px; font-size:14px; }
.review-table th, .review-table td{ border:1px solid var(--grid-strong); padding:8px 10px; text-align:start; vertical-align:top; }
.review-table thead th{ background:var(--ink); color:#fff; position:sticky; top:0; }
.review-table .pname{ font-weight:700; color:var(--ink); white-space:nowrap; background:var(--paper); }
.ans{ display:flex; align-items:center; gap:6px; flex-wrap:wrap; }
.ans .word{ font-family:"Caveat","Tajawal",cursive; font-size:19px; color:var(--ink); }
html[dir=rtl] .ans .word{ font-family:"Tajawal"; font-size:16px; }
.ans.bad .word{ color:var(--margin); text-decoration:line-through; }
.ans.rejected .word{ color:var(--margin); text-decoration:line-through; opacity:.7; }
.chip{ font-size:11px; border-radius:6px; padding:1px 6px; border:1px solid; cursor:pointer; background:#fff; }
.chip.flag{ color:var(--margin); border-color:var(--margin); }
.chip.flag.active{ background:var(--margin); color:#fff; }
.chip.note{ color:var(--muted); border-color:var(--paper-edge); cursor:default; }
.empty{ color:var(--muted); font-style:italic; }
.legend{ font-size:13px; color:var(--muted); margin:12px 0 0; line-height:1.6; }

/* ---------- results / standings ---------- */
.stand{ list-style:none; padding:0; margin:12px 0 0; display:grid; gap:8px; }
.stand li{ display:flex; align-items:center; gap:12px; padding:12px 14px; background:var(--paper); border-radius:10px; border:1px solid var(--paper-edge); }
.stand .rank{ font-family:"Aref Ruqaa",serif; font-size:20px; color:var(--muted); width:28px; text-align:center; }
.stand .nm{ flex:1; font-weight:700; color:var(--ink); }
.stand .pts{ font-family:"Aref Ruqaa",serif; font-size:22px; color:var(--green); }
.stand li.lead{ border-color:var(--amber); box-shadow:0 0 0 2px rgba(185,132,43,.25) inset; }
.stand .delta{ font-size:13px; color:var(--muted); }
.winner-banner{ text-align:center; font-family:"Aref Ruqaa",serif; font-size:clamp(26px,7vw,40px); color:var(--amber); margin:8px 0 0; }

/* round score table */
.rs{ width:100%; border-collapse:collapse; margin-top:10px; font-size:14px; }
.rs th,.rs td{ border:1px solid var(--grid-strong); padding:7px 9px; text-align:center; }
.rs th{ background:var(--ink); color:#fff; }
.rs td.nm{ text-align:start; font-weight:700; color:var(--ink); background:var(--paper); white-space:nowrap; }
.rs td.tot{ font-family:"Aref Ruqaa",serif; font-size:18px; color:var(--green); }
.p10{ color:var(--green); font-weight:700; }
.p5{ color:var(--amber); font-weight:700; }
.p0{ color:var(--muted); }

/* misc */
.center{ text-align:center; }
.mt{ margin-top:16px; }
.waiting{ text-align:center; color:var(--ink-soft); padding:26px 10px; }
.waiting .big{ font-family:"Aref Ruqaa",serif; font-size:26px; color:var(--ink); display:block; margin-bottom:6px; }
.err{ color:var(--margin); font-size:14px; margin-top:8px; }
.toplink{ position:absolute; top:14px; inset-inline-end:16px; font-size:13px; color:var(--muted); background:none; border:none; cursor:pointer; }

#toast{
  position:fixed; left:50%; bottom:24px; transform:translateX(-50%) translateY(20px);
  background:var(--graphite); color:#fff; padding:12px 18px; border-radius:10px;
  font-size:14px; opacity:0; pointer-events:none; transition:opacity .2s, transform .2s; z-index:50;
  max-width:90vw; text-align:center;
}
#toast.show{ opacity:1; transform:translateX(-50%) translateY(0); }

@media (max-width:560px){
  .sheet{ display:block; }
  .sheet thead{ display:none; }
  .sheet tbody, .sheet tr{ display:block; }
  .sheet tr{ margin-bottom:14px; border:1px solid var(--grid-strong); border-radius:10px; overflow:hidden; }
  .sheet td{ display:flex; align-items:center; border:none; border-bottom:1px solid var(--grid); }
  .sheet td.lettercol{ display:none; }
  .sheet td .catlabel{ display:block; width:84px; flex:0 0 84px; padding:0 12px; font-weight:700; color:var(--ink-soft); font-size:13px; }
  .sheet input.cell{ font-size:20px; }
}

@media (prefers-reduced-motion:reduce){
  *{ animation:none !important; transition:none !important; }
}
