:root {
  --bg: #0f1115; --panel: #161a21; --panel2: #1d222b; --border: #2a313c;
  --text: #e6e8eb; --muted: #9aa3ae; --accent: #4f8cff; --accent-ink: #fff;
  --ok: #34c77b; --bad: #ff6b6b; --warn: #f5b041;
  --code-bg: #12151b; --gutter: #586170; --sel: #2b3a55; --active: #181d26;
  --kw: #c792ea; --str: #c3e88d; --num: #f78c6c; --com: #6b7686; --def: #82aaff;
  --builtin: #ffcb6b; --op: #89ddff; --var2: #f07178; --prop: #9cdcfe;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", "Cascadia Code", Menlo, Consolas, monospace;
  --code-size: 15px;
  --app-h: 100vh;
  color-scheme: dark;
}
@supports (height: 100dvh) { :root { --app-h: 100dvh; } }

:root[data-theme="light"] {
  --bg: #f6f7f9; --panel: #ffffff; --panel2: #eef0f3; --border: #d9dde3;
  --text: #1b1f24; --muted: #5d6673; --accent: #2563eb; --accent-ink: #fff;
  --ok: #138a4b; --bad: #d23b3b; --warn: #b26b00;
  --code-bg: #ffffff; --gutter: #9aa3ae; --sel: #d6e4ff; --active: #f3f6fb;
  --kw: #8b2fc9; --str: #2b7a0b; --num: #b5530b; --com: #7c8794; --def: #1f5fd6;
  --builtin: #9a6700; --op: #0b7285; --var2: #c2255c; --prop: #1c6b8f;
  color-scheme: light;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]):not([data-theme="light"]) {
    --bg: #f6f7f9; --panel: #ffffff; --panel2: #eef0f3; --border: #d9dde3;
    --text: #1b1f24; --muted: #5d6673; --accent: #2563eb; --accent-ink: #fff;
    --ok: #138a4b; --bad: #d23b3b; --warn: #b26b00;
    --code-bg: #ffffff; --gutter: #9aa3ae; --sel: #d6e4ff; --active: #f3f6fb;
    --kw: #8b2fc9; --str: #2b7a0b; --num: #b5530b; --com: #7c8794; --def: #1f5fd6;
    --builtin: #9a6700; --op: #0b7285; --var2: #c2255c; --prop: #1c6b8f;
    color-scheme: light;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-family: var(--font); }
body { overflow: hidden; overscroll-behavior: none; -webkit-text-size-adjust: 100%; }
button, select, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
code { font-family: var(--mono); font-size: .92em; background: var(--panel2); padding: 1px 5px; border-radius: 4px; }
.muted { color: var(--muted); }
.small { font-size: 13px; line-height: 1.5; }
[hidden] { display: none !important; }

#app {
  position: fixed; left: 0; right: 0; top: 0; height: var(--app-h);
  display: flex; flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
}

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 6px; padding: 6px 8px;
  background: var(--panel); border-bottom: 1px solid var(--border); min-height: 52px;
}
.icon-btn {
  width: 40px; height: 40px; border: 0; border-radius: 10px; background: transparent;
  display: grid; place-items: center; flex: none;
}
.icon-btn:active { background: var(--panel2); }
.title { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.2; }
#probTitle { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.meta { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.meta .Easy { color: var(--ok); } .meta .Medium { color: var(--warn); } .meta .Hard { color: var(--bad); }
.btn {
  height: 40px; padding: 0 14px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--panel2); font-weight: 600; font-size: 14px; flex: none;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary.stop { background: var(--bad); border-color: var(--bad); }
.btn:disabled { opacity: .45; }
.btn:active:not(:disabled) { transform: scale(.97); }
.run-ico { margin-right: 6px; font-size: 12px; }
.timer {
  height: 34px; min-width: 62px; padding: 0 8px; border-radius: 17px; border: 1px solid var(--border);
  background: transparent; font-family: var(--mono); font-size: 13px; color: var(--muted); flex: none;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.timer.on { color: var(--accent); border-color: var(--accent); }
.timer.long { color: var(--warn); border-color: var(--warn); }
@media (max-width: 380px) { .run-lbl { display: none; } .run-ico { margin: 0; } .btn { padding: 0 11px; } .timer { min-width: 54px; } }

/* ---------- panes ---------- */
.panes { flex: 1; min-height: 0; display: flex; }
.pane { display: none; flex: 1; min-width: 0; min-height: 0; flex-direction: column; background: var(--bg); }
body[data-pane="problem"] #pane-problem,
body[data-pane="code"] #pane-code,
body[data-pane="console"] #pane-console { display: flex; }

.tabs {
  display: flex; background: var(--panel); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs button {
  flex: 1; height: 50px; border: 0; background: transparent; color: var(--muted);
  font-weight: 600; font-size: 14px; position: relative;
}
.tabs button.active { color: var(--accent); box-shadow: inset 0 2px 0 var(--accent); }
.dot { position: absolute; top: 12px; margin-left: 4px; width: 7px; height: 7px; border-radius: 50%; background: var(--accent); }
body.kb .tabs { display: none; }

/* Tablet landscape / desktop: problem + console on the left, editor on the right */
@media (min-width: 900px) and (min-height: 500px) {
  .tabs { display: none; }
  .panes { display: grid; grid-template-columns: minmax(300px, 40%) 1fr; grid-template-rows: 3fr 2fr;
           grid-template-areas: "prob code" "cons code"; }
  #pane-problem, #pane-code, #pane-console { display: flex; }
  #pane-problem { grid-area: prob; border-right: 1px solid var(--border); }
  #pane-console { grid-area: cons; border-right: 1px solid var(--border); border-top: 1px solid var(--border); }
  #pane-code { grid-area: code; }
}
/* Tablet portrait: editor on top, problem | console below */
@media (min-width: 700px) and (orientation: portrait) {
  .tabs { display: none; }
  .panes { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 3fr 2fr;
           grid-template-areas: "code code" "prob cons"; }
  #pane-problem, #pane-code, #pane-console { display: flex; }
  #pane-problem { grid-area: prob; border-top: 1px solid var(--border); border-right: 1px solid var(--border); }
  #pane-console { grid-area: cons; border-top: 1px solid var(--border); }
  #pane-code { grid-area: code; }
}

/* ---------- problem ---------- */
.problem { flex: 1; overflow-y: auto; padding: 16px 16px 32px; line-height: 1.55; font-size: 15px; -webkit-overflow-scrolling: touch; }
.problem h1 { font-size: 20px; margin: 0 0 6px; line-height: 1.3; }
.problem .tags { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.tag { font-size: 12px; padding: 2px 9px; border-radius: 999px; background: var(--panel2); color: var(--muted); }
.tag.Easy { color: var(--ok); } .tag.Medium { color: var(--warn); } .tag.Hard { color: var(--bad); }
.tag.solved { color: var(--ok); }
.problem p { margin: 0 0 10px; }
.problem .note { font-size: 13px; color: var(--muted); }
.problem .follow { font-style: italic; color: var(--muted); }
.problem h3 { font-size: 14px; margin: 18px 0 8px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.example {
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px;
  font-family: var(--mono); font-size: 13px; margin-bottom: 8px; overflow-x: auto; white-space: pre;
}
.example b { font-family: var(--font); color: var(--muted); font-weight: 600; font-size: 12px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 16px; }
.actions .chip { height: 40px; padding: 0 14px; border-radius: 20px; font-size: 14px; }
.reveal { margin-top: 12px; background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 12px; }
.reveal pre { margin: 8px 0 0; font-family: var(--mono); font-size: 13px; overflow-x: auto; }

/* ---------- editor ---------- */
#editor { flex: 1; min-height: 0; position: relative; }
.CodeMirror {
  height: 100%; background: var(--code-bg); color: var(--text);
  font-family: var(--mono); font-size: var(--code-size); line-height: 1.5;
}
.CodeMirror-gutters { background: var(--code-bg); border-right: 1px solid var(--border); }
.CodeMirror-linenumber { color: var(--gutter); padding: 0 6px 0 4px; min-width: 22px; }
.CodeMirror-cursor { border-left: 2px solid var(--accent); }
.CodeMirror-selected, .CodeMirror-focused .CodeMirror-selected { background: var(--sel) !important; }
.CodeMirror-line::selection, .CodeMirror-line > span::selection, .CodeMirror-line > span > span::selection { background: var(--sel); }
.CodeMirror-activeline-background { background: var(--active); }
.CodeMirror-matchingbracket { color: var(--ok) !important; text-decoration: underline; }
.CodeMirror-nonmatchingbracket { color: var(--bad) !important; }
.cm-s-default .cm-keyword { color: var(--kw); }
.cm-s-default .cm-string, .cm-s-default .cm-string-2 { color: var(--str); }
.cm-s-default .cm-number { color: var(--num); }
.cm-s-default .cm-comment { color: var(--com); font-style: italic; }
.cm-s-default .cm-def { color: var(--def); }
.cm-s-default .cm-builtin { color: var(--builtin); }
.cm-s-default .cm-operator { color: var(--op); }
.cm-s-default .cm-variable { color: var(--text); }
.cm-s-default .cm-variable-2 { color: var(--var2); }
.cm-s-default .cm-property { color: var(--prop); }
.cm-s-default .cm-meta { color: var(--builtin); }
.cm-s-default .cm-atom { color: var(--num); }
.cm-s-default .cm-bracket { color: var(--muted); }
.cm-s-default .cm-error { color: var(--bad); }

/* ---------- key bar ---------- */
.keybar {
  display: flex; gap: 4px; padding: 5px 6px; background: var(--panel); border-top: 1px solid var(--border);
  overflow-x: auto; overscroll-behavior-x: contain; scrollbar-width: none; flex: none;
}
.keybar::-webkit-scrollbar { display: none; }
.keybar button {
  flex: none; min-width: 40px; height: 40px; padding: 0 9px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--panel2); font-family: var(--mono); font-size: 17px;
  user-select: none; -webkit-user-select: none;
}
.keybar button.wide { font-family: var(--font); font-size: 13px; font-weight: 600; }
.keybar button:active, .keybar button.pressed { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.keybar .sep { flex: none; width: 1px; background: var(--border); margin: 4px 2px; }
body.no-keybar .keybar { display: none; }

/* ---------- console ---------- */
.console-bar { display: flex; align-items: center; gap: 6px; padding: 6px 10px; border-bottom: 1px solid var(--border); background: var(--panel); min-height: 46px; }
.spacer { flex: 1; }
.status { font-size: 12px; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; }
.status::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.status.ready::before { background: var(--ok); }
.status.loading::before, .status.busy::before { background: var(--warn); animation: pulse 1s infinite alternate; }
.status.error::before { background: var(--bad); }
@keyframes pulse { to { opacity: .3; } }
.chip {
  height: 32px; padding: 0 12px; border-radius: 16px; border: 1px solid var(--border);
  background: var(--panel2); font-size: 13px; font-weight: 600;
}
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.chip.danger { color: var(--bad); }
.stdin-wrap { padding: 8px 10px; border-bottom: 1px solid var(--border); background: var(--panel); }
.stdin-wrap label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.stdin-wrap textarea {
  width: 100%; resize: vertical; background: var(--code-bg); border: 1px solid var(--border); border-radius: 8px;
  padding: 8px; font-family: var(--mono); font-size: 16px;
}
.output {
  flex: 1; overflow: auto; padding: 10px 12px 24px; font-family: var(--mono); font-size: 13.5px; line-height: 1.5;
  white-space: pre-wrap; word-break: break-word; -webkit-overflow-scrolling: touch;
}
.output .err { color: var(--bad); }
.output .sys { color: var(--muted); }
.output .okc { color: var(--ok); }
.output .empty { color: var(--muted); font-family: var(--font); white-space: normal; }

.results { font-family: var(--font); white-space: normal; margin: 6px 0 10px; }
.summary { font-weight: 700; font-size: 15px; margin-bottom: 8px; }
.summary.pass { color: var(--ok); } .summary.fail { color: var(--bad); }
.case { border: 1px solid var(--border); border-radius: 10px; margin-bottom: 6px; background: var(--panel); overflow: hidden; }
.case summary { list-style: none; padding: 9px 12px; display: flex; gap: 8px; align-items: center; font-size: 14px; cursor: pointer; }
.case summary::-webkit-details-marker { display: none; }
.case .badge { font-weight: 700; }
.case.ok .badge { color: var(--ok); } .case.bad .badge { color: var(--bad); }
.case .ms { margin-left: auto; color: var(--muted); font-size: 12px; font-family: var(--mono); }
.case .body { padding: 0 12px 10px; font-family: var(--mono); font-size: 12.5px; }
.case .body div { margin-top: 6px; white-space: pre-wrap; word-break: break-word; }
.case .body b { font-family: var(--font); color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .04em; display: block; }
.case .body .err { color: var(--bad); }

/* ---------- drawer ---------- */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 20; }
.drawer {
  position: fixed; top: 0; bottom: 0; left: 0; width: min(380px, 90vw); z-index: 30;
  background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column;
  transform: translateX(-102%); transition: transform .22s ease;
  padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.drawer.open { transform: none; }
.drawer-head { display: flex; align-items: center; gap: 10px; padding: 8px 8px 8px 16px; border-bottom: 1px solid var(--border); }
.drawer-head strong { font-size: 17px; }
.drawer-head .muted { flex: 1; font-size: 13px; }
.drawer-body { flex: 1; overflow-y: auto; padding: 12px 12px 24px; -webkit-overflow-scrolling: touch; }
#search {
  width: 100%; height: 42px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg);
  padding: 0 12px; font-size: 16px;
}
.filters { display: flex; gap: 6px; margin: 10px 0; flex-wrap: wrap; }
.plist { list-style: none; margin: 0; padding: 0; }
.plist li button {
  width: 100%; text-align: left; border: 0; background: transparent; padding: 10px 10px; border-radius: 10px;
  display: grid; grid-template-columns: 22px 1fr auto; gap: 4px 8px; align-items: center;
}
.plist li button:active, .plist li button.current { background: var(--panel2); }
.plist .chk { color: var(--ok); font-weight: 700; }
.plist .nm { font-size: 15px; font-weight: 500; }
.plist .df { font-size: 12px; font-weight: 600; }
.plist .df.Easy { color: var(--ok); } .plist .df.Medium { color: var(--warn); } .plist .df.Hard { color: var(--bad); }
.plist .tp { grid-column: 2 / 4; font-size: 12px; color: var(--muted); }
.drawer h3 { font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; margin: 22px 4px 6px; }
.drawer h3.first { margin-top: 4px; }
.file-actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 6px 0; }
.file-actions .chip { height: 40px; padding: 0 14px; border-radius: 20px; }
.file-pick { position: relative; display: inline-flex; align-items: center; overflow: hidden; cursor: pointer; }
.file-pick input { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; cursor: pointer; font-size: 16px; }
.plist .chk.fi { color: var(--muted); font-weight: 400; }
#flist:empty { display: none; }
#flist li { display: flex; align-items: center; gap: 2px; }
#flist li > button[data-id] { flex: 1; min-width: 0; }
.plist .nm { overflow-wrap: anywhere; }
#flist li > button.more {
  flex: none; width: 44px; height: 44px; padding: 0; display: block; text-align: center;
  border: 0; border-radius: 10px; background: transparent;
  color: var(--muted); font-size: 22px; line-height: 1; letter-spacing: 1px;
}
#flist li > button.more:active { background: var(--panel2); }

/* ---------- sheets (menus & dialogs) ---------- */
.sheet-bg { z-index: 40; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41; margin: 0 auto; width: min(480px, 100%);
  background: var(--panel); border: 1px solid var(--border); border-radius: 16px 16px 0 0;
  padding: 14px 14px calc(14px + env(safe-area-inset-bottom)); box-shadow: 0 -8px 30px rgba(0,0,0,.35);
}
/* Dialogs with a text field sit at the top so the soft keyboard never covers them. */
.sheet.top {
  bottom: auto; top: calc(var(--vv-top, 0px) + env(safe-area-inset-top) + 10px);
  width: min(480px, calc(100% - 20px)); border-radius: 16px; padding-bottom: 14px;
}
.sheet-head { display: flex; flex-direction: column; gap: 2px; padding: 2px 4px 12px; }
.sheet-head b { font-size: 17px; overflow-wrap: anywhere; }
.sheet-actions { display: flex; flex-direction: column; gap: 6px; }
.sheet.top .sheet-actions { flex-direction: row; justify-content: flex-end; }
.sbtn {
  min-height: 48px; padding: 0 18px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--panel2); font-size: 16px; font-weight: 600;
}
.sbtn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.sbtn.danger { color: var(--bad); }
.name-in {
  width: 100%; height: 46px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg);
  padding: 0 12px; font-size: 16px; font-family: var(--mono); font-variant-ligatures: none;
}
.name-msg { margin: 8px 4px 12px; }
.name-msg.bad { color: var(--bad); }
body.dragging #app { outline: 3px dashed var(--accent); outline-offset: -6px; }
.setting { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 9px 4px; min-height: 48px; border-bottom: 1px solid var(--border); font-size: 15px; }
.setting input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--accent); }
.setting select { height: 36px; border-radius: 8px; border: 1px solid var(--border); background: var(--panel2); padding: 0 8px; font-size: 15px; }
.stepper { display: inline-flex; align-items: center; gap: 8px; }
.stepper b { min-width: 24px; text-align: center; font-family: var(--mono); }
.CodeMirror, .output, .example, .reveal pre, .case .body { font-variant-ligatures: none; font-feature-settings: "liga" 0, "calt" 0; }
