/* =====================================================================
 *  style.css — pygrader (โทนโมเดิร์น)
 * ===================================================================== */
:root {
  --bg: #f5f6fb;
  --bg-2: #eef1f9;
  --card: #ffffff;
  --ink: #171a22;
  --muted: #6b7488;
  --line: #e6e9f2;
  --brand: #2f7fe4;
  --brand-2: #4aa3ff;
  --brand-ink: #1c5fbf;
  --accent: #f5b301;
  --accent-dark: #e09a00;
  --ok: #12a150; --ok-bg: #e6f6ee;
  --wa: #e02424; --wa-bg: #fdeaea;
  --warn: #c2790b; --warn-bg: #fdf3e0;
  --code-bg: #0e1424; --code-ink: #e8eefc;
  --radius: 16px;
  --shadow: 0 6px 24px rgba(30, 34, 64, .07);
  --shadow-sm: 0 1px 3px rgba(30, 34, 64, .06);
}
* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  font-family: "Sarabun", "Noto Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif;
  background: radial-gradient(1200px 600px at 85% -10%, #dcebff 0, transparent 60%), radial-gradient(900px 500px at 5% -10%, #fff4d6 0, transparent 55%), var(--bg);
  color: var(--ink); line-height: 1.6;
  display: flex; flex-direction: column;
}
a { color: var(--brand-ink); text-decoration: none; }
a:hover { text-decoration: underline; }
.wrap { width: 100%; max-width: 1120px; margin: 0 auto; padding: 26px 18px 40px; flex: 1; }

/* ---- Nav ---- */
.nav {
  position: sticky; top: 0; z-index: 50;
  background: rgba(255,255,255,.82); backdrop-filter: saturate(180%) blur(10px);
  border-bottom: 1px solid var(--line);
}
.nav-inner { max-width: 1120px; margin: 0 auto; padding: 10px 18px; display: flex; align-items: center; gap: 18px; }
.brand { display: flex; align-items: center; gap: 9px; font-weight: 800; font-size: 19px; color: var(--ink); letter-spacing: -.3px; }
.brand:hover { text-decoration: none; }
.brand-logo { height: 38px; width: 38px; object-fit: contain; filter: drop-shadow(0 2px 4px rgba(30,40,80,.18)); }
.brand span { background: linear-gradient(120deg, var(--brand-ink), var(--brand)); -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; }
.nav-links { display: flex; gap: 4px; margin-left: 6px; flex: 1; flex-wrap: wrap; }
.nav-links a {
  color: var(--muted); font-weight: 600; font-size: 14.5px; padding: 7px 12px; border-radius: 9px;
}
.nav-links a:hover { background: var(--bg-2); color: var(--ink); text-decoration: none; }
.nav-auth { display: flex; align-items: center; gap: 10px; }
.avatar { width: 32px; height: 32px; border-radius: 50%; object-fit: cover; background: var(--bg-2); border: 1px solid var(--line); }
.uname { font-size: 14px; font-weight: 600; max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---- ปุ่ม ---- */
.btn, button.btn {
  font: inherit; font-weight: 700; cursor: pointer; border: none; color: #fff;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  border-radius: 11px; padding: 11px 18px; box-shadow: 0 4px 14px rgba(91,91,246,.28);
  transition: transform .06s ease, box-shadow .15s ease, opacity .15s;
}
.btn:hover { transform: translateY(-1px); box-shadow: 0 8px 20px rgba(91,91,246,.34); }
.btn.sm { padding: 8px 13px; border-radius: 9px; font-size: 14px; box-shadow: none; }
.btn-ghost { font: inherit; font-weight: 700; cursor: pointer; border: 1px solid var(--line); color: var(--brand-ink); background: #fff; border-radius: 11px; padding: 10px 16px; }
.btn-ghost:hover { background: var(--bg-2); }
.btn-ghost.sm { padding: 7px 12px; font-size: 14px; border-radius: 9px; }
.btn-danger { background: var(--wa); box-shadow: none; }
.btn-danger:hover { background: #b91c1c; }
button:disabled { opacity: .5; cursor: not-allowed; transform: none; }

/* ---- การ์ด ---- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 22px; margin-bottom: 18px; box-shadow: var(--shadow-sm); }
h1.page { font-size: 26px; margin: 4px 0 2px; letter-spacing: -.5px; }
.lead { color: var(--muted); margin: 0 0 20px; }
h2 { font-size: 18px; margin: 0 0 14px; letter-spacing: -.2px; }

/* ---- Login gate ---- */
.login-gate { flex: 1; display: flex; align-items: center; justify-content: center; padding: 60px 18px; }
.login-card { background: var(--card); border: 1px solid var(--line); border-radius: 22px; box-shadow: var(--shadow); padding: 44px 40px; text-align: center; max-width: 440px; }
.login-card .logo { font-size: 46px; }
.login-card h1 { font-size: 24px; margin: 12px 0 6px; }
.login-card p { color: var(--muted); margin: 0 0 22px; }
.gbtn { display: inline-flex; align-items: center; gap: 10px; font: inherit; font-weight: 700; cursor: pointer; background: #fff; color: #3c4043; border: 1px solid #dadce0; border-radius: 12px; padding: 12px 22px; box-shadow: var(--shadow-sm); }
.gbtn:hover { background: #f8f9fa; }
.gbtn svg { width: 20px; height: 20px; }

/* ---- หมวดหมู่ + รายการโจทย์ ---- */
.cat-head { display: flex; align-items: center; gap: 10px; margin: 26px 0 12px; }
.cat-head h2 { margin: 0; }
.cat-count { color: var(--muted); font-size: 13.5px; }
.task-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 14px; }
.task-item { position: relative; display: block; border: 1px solid var(--line); border-radius: 14px; padding: 16px 16px 15px; background: #fff; box-shadow: var(--shadow-sm); transition: .16s; }
.task-item:hover { border-color: #cdd0ff; box-shadow: 0 8px 22px rgba(91,91,246,.14); transform: translateY(-2px); text-decoration: none; }
.task-item .t { font-weight: 700; font-size: 16px; color: var(--ink); padding-right: 26px; }
.task-item .meta { color: var(--muted); font-size: 13px; margin-top: 8px; display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.solved { position: absolute; top: 14px; right: 14px; width: 22px; height: 22px; border-radius: 50%; background: var(--ok-bg); color: var(--ok); display: grid; place-items: center; font-size: 13px; font-weight: 800; }
.chip { display: inline-block; font-size: 12px; font-weight: 700; padding: 3px 10px; border-radius: 999px; background: #eef0fe; color: var(--brand-ink); }
.stars { color: #f5a623; letter-spacing: 1px; }

/* ---- หน้าโจทย์ ---- */
.layout { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; align-items: start; }
@media (max-width: 900px) { .layout { grid-template-columns: 1fr; } }
.statement code { background: #eef0fe; padding: 1px 7px; border-radius: 6px; }
.samples table { width: 100%; border-collapse: collapse; font-size: 14px; }
.samples th, .samples td { border: 1px solid var(--line); padding: 9px; text-align: left; vertical-align: top; }
.samples th { background: var(--bg-2); font-weight: 700; }
.samples pre, .cases pre { margin: 0; white-space: pre-wrap; font-family: ui-monospace, "Cascadia Code", Consolas, monospace; }

.toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 10px; flex-wrap: wrap; }
textarea#code {
  width: 100%; min-height: 340px; resize: vertical;
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: 14px;
  background: var(--code-bg); color: var(--code-ink); caret-color: #fff;
  border: 1px solid #24304a; border-radius: 12px; padding: 15px; tab-size: 4; line-height: 1.55;
}
input[type=text], input[type=number], select, textarea.field {
  padding: 9px 11px; border: 1px solid var(--line); border-radius: 9px; font: inherit; background: #fff; color: var(--ink);
}
input:focus, select:focus, textarea:focus { outline: 2px solid #c9caff; border-color: var(--brand); }
label.fld { display: block; font-weight: 600; font-size: 14px; margin: 12px 0 5px; }
.status { font-size: 14px; color: var(--muted); min-height: 22px; margin: 10px 0; }

/* ---- ผลตรวจ ---- */
.result-summary { font-size: 16px; font-weight: 800; padding: 13px 15px; border-radius: 12px; margin-bottom: 12px; }
.badge { display: inline-block; font-size: 12px; font-weight: 800; padding: 3px 10px; border-radius: 7px; }
.b-AC { background: var(--ok-bg); color: var(--ok); }
.b-WA, .b-RE { background: var(--wa-bg); color: var(--wa); }
.b-TLE, .b-PARTIAL { background: var(--warn-bg); color: var(--warn); }
.b-PENDING { background: var(--bg-2); color: var(--muted); }
.cases { list-style: none; padding: 0; margin: 0; }
.cases li { border: 1px solid var(--line); border-radius: 11px; padding: 11px 13px; margin-bottom: 9px; }
.cases .head { display: flex; justify-content: space-between; align-items: center; gap: 8px; font-weight: 600; }
.cases .diff { margin-top: 8px; font-size: 13px; }
.cases .diff pre { background: var(--bg-2); border: 1px solid var(--line); border-radius: 8px; padding: 8px; margin: 4px 0; overflow-x: auto; }
.hidden-note { color: var(--muted); font-style: italic; font-size: 13px; }

/* ---- ตาราง (ประวัติ / admin) ---- */
.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
.tbl th, .tbl td { padding: 10px 12px; border-bottom: 1px solid var(--line); text-align: left; }
.tbl th { color: var(--muted); font-weight: 700; font-size: 13px; text-transform: uppercase; letter-spacing: .3px; }
.tbl tr:hover td { background: #fafbff; }
.row-actions { display: flex; gap: 8px; }

/* ---- admin testcase editor ---- */
.tc-row { display: grid; grid-template-columns: 1fr 1fr 90px 70px 34px; gap: 8px; align-items: start; margin-bottom: 8px; }
.tc-row textarea { min-height: 58px; resize: vertical; }
.tc-head { font-size: 12px; color: var(--muted); font-weight: 700; }
@media (max-width: 760px) { .tc-row { grid-template-columns: 1fr 1fr; } .tc-head { display: none; } }

/* ---- footer ---- */
.site-footer {
  text-align: center; color: var(--muted); font-size: 13.5px;
  padding: 22px 18px; border-top: 1px solid var(--line); background: rgba(255,255,255,.6);
}
.site-footer a { color: var(--brand-ink); }

.empty { color: var(--muted); padding: 12px 2px; }
.grow { flex: 1; }

/* =====================================================================
 *  เพิ่มเติม: แก้ [hidden], nav active, หน้าแรก/สถิติ, filterbar, signin
 * ===================================================================== */

/* สำคัญ: ให้ hidden ชนะเสมอ (แก้ปัญหากล่อง login ไม่ซ่อน) */
[hidden] { display: none !important; }

/* เมนูที่ active */
.nav-links a.active { background: #eef0fe; color: var(--brand-ink); }

/* ปุ่มเมนูมือถือ (ซ่อนบนจอใหญ่) */
.nav-toggle { display: none; background: none; border: none; font-size: 22px; line-height: 1; cursor: pointer; color: var(--ink); padding: 6px 10px; border-radius: 9px; }
.nav-toggle:hover { background: var(--bg-2); }

@media (max-width: 720px) {
  .nav-inner { gap: 8px; }
  .nav-toggle { display: block; order: 3; }
  .nav-auth { order: 2; margin-left: auto; gap: 8px; }
  .nav-auth .uname { display: none; }               /* ประหยัดพื้นที่บนมือถือ */
  .nav-links { order: 4; width: 100%; flex: none; flex-direction: column; align-items: stretch; gap: 2px; margin: 6px 0 2px; display: none; }
  .nav.open .nav-links { display: flex; }
  .nav-links a { padding: 11px 12px; border-radius: 10px; }
}

/* กล่องแจ้งเข้าสู่ระบบ (ไม่กลางจอ) */
.signin-note {
  display: flex; align-items: center; gap: 16px;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow-sm); padding: 22px 24px; margin-top: 8px;
}
.signin-logo { width: 84px; height: 84px; object-fit: contain; flex-shrink: 0; filter: drop-shadow(0 4px 10px rgba(30,40,80,.18)); }

/* hero หน้าแรก */
.hero { display: flex; align-items: center; gap: 16px; margin-bottom: 18px; }
.hero .btn { white-space: nowrap; }
@media (max-width: 560px) { .hero { flex-direction: column; align-items: flex-start; } }

/* KPI */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 18px; }
@media (max-width: 820px) { .kpis { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 460px) { .kpis { grid-template-columns: 1fr; } }
.kpi { display: flex; align-items: center; gap: 14px; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow-sm); padding: 16px 18px; }
.kpi-ic { font-size: 26px; width: 46px; height: 46px; display: grid; place-items: center; background: #eef0fe; border-radius: 12px; }
.kpi-num { font-size: 24px; font-weight: 800; letter-spacing: -.5px; }
.kpi-label { color: var(--muted); font-size: 13.5px; }

/* กระดานอันดับ */
.board-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; align-items: start; }
@media (max-width: 820px) { .board-grid { grid-template-columns: 1fr; } }
.board { list-style: none; margin: 0; padding: 0; counter-reset: none; }
.board-row { display: flex; align-items: center; gap: 12px; padding: 11px 6px; border-bottom: 1px solid var(--line); }
.board-row:last-child { border-bottom: none; }
.board-row .rank { width: 30px; text-align: center; font-weight: 800; color: var(--muted); font-size: 15px; }
.board-row.top3 .rank { font-size: 20px; }
.board-row .who { flex: 1; font-weight: 600; font-variant-numeric: tabular-nums; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.board-row .stat { font-size: 13.5px; white-space: nowrap; }
.board-row .stat.muted { color: var(--muted); }
.board-row.top3 { background: linear-gradient(90deg, #faf8ff, transparent); border-radius: 10px; }

/* แถบค้นหา/กรอง คลังโจทย์ */
.filterbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 18px;
  position: sticky; top: 58px; z-index: 20; background: rgba(245,246,251,.9); backdrop-filter: blur(6px); padding: 8px 0; }
.search { position: relative; flex: 1; min-width: 200px; }
.search-ic { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); opacity: .6; }
.search input { width: 100%; padding: 10px 12px 10px 36px; border: 1px solid var(--line); border-radius: 11px; font: inherit; background: #fff; }
.filterbar select { padding: 10px 12px; border: 1px solid var(--line); border-radius: 11px; font: inherit; background: #fff; }
.count { color: var(--muted); font-size: 13.5px; margin-left: auto; }

/* =====================================================================
 *  ตารางคลังโจทย์ (แบบยาว ค้นหา/เรียงได้)
 * ===================================================================== */
.table-wrap { overflow-x: auto; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow-sm); }
table.problems { width: 100%; border-collapse: collapse; font-size: 14.5px; min-width: 680px; }
table.problems thead th {
  background: #f7f9fd; color: var(--muted);
  font-weight: 700; font-size: 13px; text-align: left; padding: 12px 14px;
  border-bottom: 2px solid var(--line); white-space: nowrap; cursor: pointer; user-select: none;
}
table.problems thead th.no-sort { cursor: default; }
table.problems thead th .arrow { color: var(--brand); font-size: 11px; margin-left: 3px; }
table.problems tbody td { padding: 12px 14px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.problems tbody tr { cursor: pointer; transition: background .12s; }
table.problems tbody tr:hover td { background: #f2f7ff; }
table.problems tbody tr:last-child td { border-bottom: none; }
.p-title { font-weight: 700; color: var(--ink); }
.p-slug { color: var(--muted); font-size: 12px; }
.p-num { text-align: center; color: var(--muted); font-variant-numeric: tabular-nums; }
.st-done { color: var(--ok); font-weight: 700; white-space: nowrap; }
.st-best { color: var(--accent-dark); font-weight: 700; white-space: nowrap; }
.st-none { color: var(--muted); }
.idx { color: var(--muted); width: 42px; text-align: center; font-variant-numeric: tabular-nums; }

/* เน้นหัวข้อด้วยขีดสีเหลืองใต้ */
h1.page { position: relative; display: inline-block; }
h1.page::after { content: ""; position: absolute; left: 0; bottom: -4px; width: 46px; height: 4px; border-radius: 3px; background: linear-gradient(90deg, var(--accent), var(--brand)); }

/* เหรียญทองอันดับ 1-3 ใช้โทนเหลือง */
.board-row.top3 { background: linear-gradient(90deg, #fff8e6, transparent); }

/* สวิตช์ เปิด/ปิด */
.switch { position: relative; display: inline-block; width: 50px; height: 28px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .slider { position: absolute; inset: 0; cursor: pointer; background: #cbd2e0; border-radius: 999px; transition: .2s; }
.switch .slider::before { content: ""; position: absolute; height: 22px; width: 22px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: .2s; box-shadow: 0 1px 3px rgba(0,0,0,.25); }
.switch input:checked + .slider { background: linear-gradient(135deg, var(--brand), var(--brand-2)); }
.switch input:checked + .slider::before { transform: translateX(22px); }

/* โปรแกรมแก้ไขโค้ด CodeMirror */
.CodeMirror {
  border-radius: 12px; overflow: hidden; border: 1px solid #24304a;
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 14px; line-height: 1.6; height: auto;
}
.CodeMirror-scroll { min-height: 340px; }
.CodeMirror-gutters { border-right: 1px solid #2a3552; }
