:root {
  color-scheme: light;
  --bg: #f4f3f0;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #dddbd5;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7a7973;
  --accent: #cd412b;
  --accent-ink: #ffffff;
  --good: #1a7f37;
  --bad: #b42318;
  --warn-bg: #fff4d6;
  --warn-ink: #6b4a00;
  --err-bg: #fde8e7;
  --err-ink: #8c1d18;
  --seq: #1c5cab;
  --cell-empty: #e8eef6;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 4px 16px rgba(0, 0, 0, 0.05);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #232321;
    --border: #34332f;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --muted: #8f8e86;
    --accent: #e0573f;
    --good: #3fb950;
    --bad: #f47067;
    --warn-bg: #3a2e0d;
    --warn-ink: #f2d27a;
    --err-bg: #3d1614;
    --err-ink: #ffb4ab;
    --seq: #5598e7;
    --cell-empty: #22262c;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211;
  --surface: #1a1a19;
  --surface-2: #232321;
  --border: #34332f;
  --text: #ffffff;
  --text-2: #c3c2b7;
  --muted: #8f8e86;
  --accent: #e0573f;
  --good: #3fb950;
  --bad: #f47067;
  --warn-bg: #3a2e0d;
  --warn-ink: #f2d27a;
  --err-bg: #3d1614;
  --err-ink: #ffb4ab;
  --seq: #5598e7;
  --cell-empty: #22262c;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3, h4 { margin: 0; line-height: 1.2; }
h2 { font-size: 18px; }
h3 { font-size: 15px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-variant-numeric: tabular-nums; }
.hidden { display: none !important; }

/* ---------- header ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 1200px; margin: 0 auto; padding: 10px 16px;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; }
.brand-mark {
  width: 26px; height: 26px; border-radius: 6px; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; font-size: 14px; font-weight: 800;
}
.server-picker { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; }
.server-picker select { max-width: 100%; min-width: 0; flex: 1 1 auto; }
.top-actions { display: flex; gap: 6px; margin-left: auto; }

.tabs {
  max-width: 1200px; margin: 0 auto; padding: 0 16px;
  display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  border: 0; background: none; padding: 10px 12px; cursor: pointer; color: var(--text-2);
  border-bottom: 2px solid transparent; white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.tab .count {
  display: inline-block; min-width: 18px; padding: 0 5px; margin-left: 4px; border-radius: 9px;
  background: var(--surface-2); font-size: 11px; text-align: center;
}

main { max-width: 1200px; margin: 0 auto; padding: 16px; }

/* ---------- generic UI ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface); padding: 6px 12px;
  border-radius: 8px; cursor: pointer; white-space: nowrap; text-decoration: none;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--bad); }
.btn.sm { padding: 3px 8px; font-size: 12px; border-radius: 6px; }
.btn.icon { padding: 6px 8px; }
.btn.ghost { border-color: transparent; background: none; }
.btn.ghost:hover { background: var(--surface-2); }

input[type="text"], input[type="search"], input[type="password"], input[type="number"], input[type="url"], select, textarea {
  border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 6px 10px; min-width: 0;
}
textarea { width: 100%; resize: vertical; min-height: 60px; }
input:focus-visible, select:focus-visible, textarea:focus-visible, .btn:focus-visible, .tab:focus-visible {
  outline: 2px solid var(--seq); outline-offset: 1px;
}
label.field { display: grid; gap: 4px; font-size: 13px; color: var(--text-2); }
label.check { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 16px; box-shadow: var(--shadow);
}
.stack { display: grid; gap: 16px; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.between { justify-content: space-between; }
.grid-cards { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }

.notice { padding: 10px 12px; border-radius: 8px; font-size: 13px; }
.notice.warn { background: var(--warn-bg); color: var(--warn-ink); }
.notice.err { background: var(--err-bg); color: var(--err-ink); }
.notice.info { background: var(--surface-2); color: var(--text-2); }

.empty { text-align: center; padding: 32px 16px; color: var(--muted); }
.empty h3 { color: var(--text); margin-bottom: 6px; }

/* 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: 12px; padding: 12px 14px; }
.tile .label { font-size: 12px; color: var(--text-2); }
.tile .value { font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: 2px; }
.tile .sub { font-size: 12px; color: var(--muted); }

/* status */
.status { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; background: var(--muted); }
.dot.on { background: var(--good); box-shadow: 0 0 0 3px color-mix(in srgb, var(--good) 22%, transparent); }
.dot.off { background: transparent; border: 1.5px solid var(--muted); }
.status.on { color: var(--good); font-weight: 600; }

.clan-chip {
  display: inline-flex; align-items: center; gap: 5px; padding: 1px 8px 1px 6px; border-radius: 10px;
  background: var(--surface-2); font-size: 12px; color: var(--text); white-space: nowrap; max-width: 100%;
  overflow: hidden; text-overflow: ellipsis;
}
.swatch { width: 8px; height: 8px; border-radius: 2px; flex: none; }

/* tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12px; font-weight: 600; color: var(--text-2); white-space: nowrap; }
tbody tr:hover { background: var(--surface-2); }
td.actions { text-align: right; white-space: nowrap; }
.name-cell { font-weight: 600; word-break: break-word; }
.linklike { background: none; border: 0; padding: 0; cursor: pointer; font-weight: 600; text-align: left; }
.linklike:hover { text-decoration: underline; }

/* clan cards */
.clan-card { border-top: 3px solid var(--clan, var(--border)); display: grid; gap: 10px; }
.clan-card .members { display: flex; flex-wrap: wrap; gap: 6px; }
.member-pill {
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 8px; border-radius: 8px;
  background: var(--surface-2); border: 0; cursor: pointer; font-size: 13px;
}
.member-pill:hover { outline: 1px solid var(--border); }
.big-count { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }

/* activity */
.activity { list-style: none; margin: 0; padding: 0; }
.activity li { display: flex; gap: 10px; align-items: baseline; padding: 7px 0; border-bottom: 1px solid var(--border); }
.activity li:last-child { border-bottom: 0; }
.activity time { color: var(--muted); font-variant-numeric: tabular-nums; flex: none; min-width: 52px; white-space: nowrap; }
.activity .what { flex: 1; min-width: 0; }
.day-head { font-size: 12px; font-weight: 600; color: var(--text-2); margin: 14px 0 2px; text-transform: uppercase; letter-spacing: 0.04em; }

/* ---------- charts ---------- */
.heatmap { display: grid; grid-template-columns: 34px repeat(24, minmax(0, 1fr)); gap: 2px; font-size: 10px; color: var(--muted); }
.heatmap .hl { text-align: center; font-variant-numeric: tabular-nums; }
.heatmap .dl { display: flex; align-items: center; font-size: 11px; color: var(--text-2); }
.heatmap .cell { aspect-ratio: 1; border-radius: 3px; background: var(--cell-empty); min-height: 10px; }
.heatmap .cell.nodata {
  background: repeating-linear-gradient(135deg, var(--surface-2) 0 3px, transparent 3px 6px);
  outline: 1px solid var(--border); outline-offset: -1px;
}
.legend { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; font-size: 12px; color: var(--text-2); margin-top: 8px; }
.legend .ramp { width: 120px; height: 10px; border-radius: 3px; background: linear-gradient(90deg, var(--cell-empty), var(--seq)); }
.legend .nd { width: 14px; height: 10px; border-radius: 2px; background: repeating-linear-gradient(135deg, var(--surface-2) 0 3px, transparent 3px 6px); outline: 1px solid var(--border); outline-offset: -1px; }

.bars { display: flex; align-items: flex-end; gap: 2px; height: 120px; border-bottom: 1px solid var(--border); }
.bars .bar-col { flex: 1; height: 100%; display: flex; align-items: flex-end; }
.bars .bar { width: 100%; background: var(--seq); border-radius: 4px 4px 0 0; min-height: 0; }
.bars-x { display: flex; gap: 2px; font-size: 10px; color: var(--muted); margin-top: 4px; }
.bars-x span { flex: 1; text-align: center; overflow: hidden; }

.timeline { display: grid; grid-template-columns: minmax(80px, 140px) 1fr; gap: 6px 10px; align-items: center; font-size: 12px; }
.timeline .track { position: relative; height: 14px; background: var(--cell-empty); border-radius: 3px; overflow: hidden; }
.timeline .seg { position: absolute; top: 0; bottom: 0; background: var(--seq); border-radius: 3px; }
.timeline .gap { position: absolute; top: 0; bottom: 0; background: repeating-linear-gradient(135deg, var(--surface-2) 0 3px, transparent 3px 6px); }
.timeline .tl-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.timeline-axis { display: flex; justify-content: space-between; font-size: 10px; color: var(--muted); }

.insights { display: grid; gap: 8px; }
.insight { display: flex; gap: 10px; align-items: baseline; }
.insight b { min-width: 150px; color: var(--text-2); font-weight: 500; }

#tooltip {
  position: fixed; z-index: 100; pointer-events: none; padding: 6px 9px; border-radius: 6px;
  background: var(--text); color: var(--bg); font-size: 12px; max-width: 240px; white-space: pre-line;
  box-shadow: var(--shadow); opacity: 0; transition: opacity 0.08s;
}
#tooltip.show { opacity: 1; }

/* ---------- modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, 0.45);
  display: flex; align-items: flex-start; justify-content: center; padding: 40px 16px; overflow-y: auto;
}
.modal {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; width: 100%; max-width: 560px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}
.modal.wide { max-width: 900px; }
.modal-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding: 16px 18px; border-bottom: 1px solid var(--border); }
.modal-body { padding: 16px 18px; display: grid; gap: 16px; }
.modal-foot { padding: 12px 18px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }

.toast-wrap { position: fixed; bottom: 16px; right: 16px; left: 16px; z-index: 120; display: grid; justify-items: end; gap: 8px; pointer-events: none; }
.toast {
  pointer-events: auto; background: var(--surface); border: 1px solid var(--border); border-left: 3px solid var(--seq);
  padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); max-width: 360px; font-size: 13px;
}
.toast.on { border-left-color: var(--good); }
.toast.off { border-left-color: var(--muted); }
.toast.err { border-left-color: var(--bad); }

.color-options { display: flex; gap: 6px; flex-wrap: wrap; }
.color-options label { cursor: pointer; }
.color-options input { position: absolute; opacity: 0; pointer-events: none; }
.color-options span { display: block; width: 26px; height: 26px; border-radius: 6px; border: 2px solid transparent; }
.color-options input:checked + span { border-color: var(--text); }
.color-options input:focus-visible + span { outline: 2px solid var(--seq); outline-offset: 1px; }

@media (max-width: 640px) {
  .topbar-inner { gap: 8px; }
  .server-picker { order: 3; flex-basis: 100%; }
  .insight { flex-direction: column; gap: 0; }
  .insight b { min-width: 0; }
  .hide-sm { display: none; }
  th, td { padding: 8px 6px; }
  .modal-backdrop { padding: 12px 8px; }
}

.badge-new {
  display: inline-block; padding: 0 6px; margin-left: 4px; border-radius: 4px; font-size: 10px; font-weight: 700;
  letter-spacing: 0.04em; background: var(--seq); color: #fff; vertical-align: middle;
}
details > summary { color: var(--text-2); }
