:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --border: #dcdfe5;
  --text: #171a1f;
  --muted: #5d6673;
  --accent: #c8640c;
  --accent-text: #ffffff;
  --ok: #1a7f4b;
  --ok-bg: #dff3e7;
  --warn: #8a5a00;
  --warn-bg: #fbeccb;
  --bad: #b3261e;
  --bad-bg: #fbe1df;
  --idle-bg: #e6e8ec;
  --log-bg: #14171b;
  --log-text: #d6dae0;
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418;
    --surface: #1a1e24;
    --surface-2: #20252c;
    --border: #2e353f;
    --text: #e8ebef;
    --muted: #98a2b0;
    --accent: #f08a24;
    --accent-text: #17110a;
    --ok: #5fd08f;
    --ok-bg: #173526;
    --warn: #f0c060;
    --warn-bg: #3a2e12;
    --bad: #ff8a80;
    --bad-bg: #3d1c1a;
    --idle-bg: #2a3038;
    --log-bg: #0c0e11;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

h1 { font-size: 1.15rem; margin: 0; letter-spacing: 0.01em; }
h2 { font-size: 0.95rem; margin: 0 0 12px; }
code { font-family: ui-monospace, "JetBrains Mono", Menlo, Consolas, monospace; font-size: 0.9em; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.error { color: var(--bad); margin: 0; }

/* Buttons and inputs */
button {
  white-space: nowrap;
  font: inherit;
  font-weight: 600;
  padding: 8px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--muted); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button:focus-visible, input:focus-visible, .logs:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.danger { color: var(--bad); border-color: var(--bad); background: transparent; }
button.ghost { background: transparent; font-weight: 500; padding: 6px 12px; }
button.small-btn { padding: 2px 8px; font-size: 0.8rem; }

input[type="text"], input[type="password"], input[type="number"] {
  font: inherit;
  width: 100%;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
label { font-size: 0.85rem; color: var(--muted); display: block; margin-bottom: 4px; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; display: grid; gap: 10px; }
.login-card h1 { font-size: 1.3rem; }
.login-card p { margin: 0 0 6px; }
.login-card button { margin-top: 6px; }

/* Layout */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 2;
}
.title { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.page { max-width: 1040px; margin: 0 auto; padding: 20px 16px 48px; display: grid; gap: 16px; }
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  min-width: 0;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.card-head h2 { margin: 0; }

.pill {
  font-size: 0.8rem; font-weight: 600;
  padding: 3px 10px; border-radius: 999px;
  background: var(--idle-bg); color: var(--muted);
}
.pill.ok { background: var(--ok-bg); color: var(--ok); }
.pill.warn { background: var(--warn-bg); color: var(--warn); }
.pill.bad { background: var(--bad-bg); color: var(--bad); }

.banner {
  margin: 0; padding: 10px 14px; border-radius: 8px;
  background: var(--warn-bg); color: var(--warn);
}
.banner.bad { background: var(--bad-bg); color: var(--bad); }
.banner.ok { background: var(--ok-bg); color: var(--ok); }

/* Stat tiles */
.tiles { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.tile {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; display: grid; gap: 2px;
}
.tile .label { font-size: 0.8rem; color: var(--muted); }
.tile .value { font-size: 1.5rem; font-weight: 650; font-variant-numeric: tabular-nums; line-height: 1.2; }
.tile .sub { font-size: 0.8rem; color: var(--muted); min-height: 1.2em; }

/* Controls */
.buttons { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.buttons.end { justify-content: flex-end; margin: 16px 0 0; }
.row { display: flex; gap: 10px; align-items: center; }
.inline-form { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--border); }
.check { display: flex; gap: 6px; align-items: center; margin: 0; white-space: nowrap; }

/* Facts */
.facts { margin: 0; display: grid; gap: 8px; }
.facts div { display: grid; grid-template-columns: 110px 1fr; gap: 10px; align-items: baseline; }
.facts dt { color: var(--muted); font-size: 0.85rem; }
.facts dd { margin: 0; overflow-wrap: anywhere; }

/* Saves */
.saves { display: grid; gap: 16px; }
.session-name { font-weight: 600; margin: 0 0 6px; display: flex; gap: 8px; align-items: center; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th { text-align: left; font-weight: 500; color: var(--muted); font-size: 0.8rem; }
th, td { padding: 7px 10px 7px 0; border-bottom: 1px solid var(--border); white-space: nowrap; }
td.actions { text-align: right; padding-right: 0; }
td.actions button { margin-left: 6px; }
tr:last-child td { border-bottom: 0; }

/* Options */
.options { display: grid; gap: 10px; }
.option { display: grid; grid-template-columns: 1fr auto; gap: 12px; align-items: center; }
.option .name { overflow-wrap: anywhere; }
.option .key { font-size: 0.75rem; color: var(--muted); display: block; }
.option input[type="text"], .option input[type="number"] { width: 140px; }
.option input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); }

/* Log */
.logs {
  margin: 0; padding: 12px;
  background: var(--log-bg); color: var(--log-text);
  border-radius: 8px;
  font: 12px/1.5 ui-monospace, "JetBrains Mono", Menlo, Consolas, monospace;
  height: 340px; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere;
}

/* Dialog */
dialog {
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
  padding: 20px; max-width: 420px; width: calc(100% - 32px);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.5); }
dialog h2 { font-size: 1.05rem; margin-bottom: 8px; }
dialog p { margin: 0; color: var(--muted); }

@media (max-width: 520px) {
  .topbar { padding: 10px 16px; }
  .facts div { grid-template-columns: 96px 1fr; }
  .tile .value { font-size: 1.3rem; }
}
