:root {
  color-scheme: light dark;
  --bg: #f5f6f8;
  --surface: #ffffff;
  --border: #dde1e6;
  --text: #1d2125;
  --muted: #626f7d;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --danger: #c62828;
  --ok: #1b7f3b;
  --ok-bg: #e5f4ea;
  --error-bg: #fdecea;
  --muted-bg: #eef0f3;
  --radius: 8px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15181c;
    --surface: #1e2227;
    --border: #343a42;
    --text: #e6e8eb;
    --muted: #9aa4af;
    --accent: #5b8def;
    --accent-text: #0b1020;
    --danger: #ef6b6b;
    --ok: #6fcf8e;
    --ok-bg: #1d3326;
    --error-bg: #3a2020;
    --muted-bg: #2a2f36;
  }
}

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.6 system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", sans-serif;
}

h1 { font-size: 20px; margin: 0 0 16px; }
h2 { font-size: 18px; margin: 0; }
h3 { font-size: 15px; margin: 0 0 12px; }
code { background: var(--muted-bg); padding: 1px 5px; border-radius: 4px; font-size: 13px; }
a { color: var(--accent); }

button, input, select {
  font: inherit;
  color: inherit;
}

button {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 6px;
  padding: 6px 14px;
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--muted); }
button:disabled { opacity: .55; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
button.danger { color: var(--danger); }
button.small { padding: 2px 10px; font-size: 13px; }
button.link { border: none; background: none; color: var(--accent); padding: 0 0 0 12px; }

input, select {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label { display: block; margin-bottom: 12px; color: var(--muted); font-size: 13px; }
label input, label select { margin-top: 4px; color: var(--text); font-size: 14px; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; }
.error { color: var(--danger); min-height: 1em; margin: 0 0 8px; white-space: pre-wrap; }
.error:empty, .ok:empty { display: none; }
.ok { color: var(--ok); margin: 0 0 8px; }
.hint { color: var(--muted); margin: 0 0 16px; }

/* ログイン */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; }
.login-card button { width: 100%; margin-top: 4px; }

/* ヘッダー */
.topbar {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 24px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; }
.tabs { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
.tabs button { border: none; background: none; padding: 6px 12px; color: var(--muted); border-radius: 6px; }
.tabs button.active { background: var(--muted-bg); color: var(--text); font-weight: 600; }
.me { color: var(--muted); white-space: nowrap; }

.tab { max-width: 1200px; margin: 0 auto; padding: 24px; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; }

/* 合計 */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; margin-bottom: 20px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.tile-label { color: var(--muted); font-size: 13px; }
.tile-value { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.3; }
.tile-sub { color: var(--muted); font-size: 12px; }
.cell-sub { color: var(--muted); font-size: 11px; white-space: nowrap; }

/* 表 */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table.data { width: 100%; border-collapse: collapse; }
table.data th, table.data td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
table.data th { color: var(--muted); font-weight: 600; font-size: 13px; white-space: nowrap; }
table.data tbody tr:last-child td { border-bottom: none; }
.num { text-align: right !important; font-variant-numeric: tabular-nums; white-space: nowrap; }
.stale { color: var(--muted); }
.nowrap { white-space: nowrap; }
.break { word-break: break-all; }
.empty { color: var(--muted); text-align: center !important; }
.actions-cell { text-align: right !important; white-space: nowrap; }
.actions-cell button + button { margin-left: 6px; }
.instance-name { font-weight: 600; }
.instance-url { font-size: 12px; color: var(--muted); word-break: break-all; }
.error-detail { color: var(--danger); font-size: 12px; margin-top: 4px; max-width: 280px; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; white-space: nowrap; }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.error { background: var(--error-bg); color: var(--danger); }
.badge.muted { background: var(--muted-bg); color: var(--muted); }
.badge.self { margin-left: 6px; }

.form-card { max-width: 420px; margin-top: 16px; }

/* ダイアログ */
dialog {
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text);
  padding: 24px; width: min(440px, calc(100vw - 32px));
}
dialog::backdrop { background: rgb(0 0 0 / .4); }
.actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }

@media (max-width: 640px) {
  .topbar { padding: 10px 16px; gap: 8px; }
  .brand { flex: 1; }
  .tabs { order: 1; flex-basis: 100%; overflow-x: auto; flex-wrap: nowrap; }
  .tabs button { white-space: nowrap; }
  .instance-name { min-width: 8em; }
  .tile-value { font-size: 22px; }
  .tab { padding: 16px; }
}

/* 利用状況の内訳 */
button.link.back { padding: 0; margin-bottom: 8px; }
.detail-meta { color: var(--muted); font-size: 13px; margin-top: 2px; word-break: break-all; }
.notice { padding: 10px 14px; border-radius: var(--radius); background: var(--error-bg); margin: 0 0 16px; }
.subtabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.subtabs button { border: none; border-bottom: 2px solid transparent; border-radius: 0; background: none; padding: 8px 14px; color: var(--muted); margin-bottom: -1px; }
.subtabs button.active { border-bottom-color: var(--accent); color: var(--text); font-weight: 600; }
.workspace { margin-bottom: 24px; }
.workspace-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.workspace-head h3 { margin: 0; }
.workspace-sum { color: var(--muted); font-size: 13px; }
.channel-name { word-break: break-all; min-width: 10em; }
/* ワークスペースごとの表の列幅を揃える */
.channels-table th:nth-child(2) { width: 90px; }
.channels-table th:nth-child(3), .channels-table th:nth-child(5), .channels-table th:nth-child(6) { width: 90px; }
.channels-table th:nth-child(7) { width: 100px; }
#detail-refresh { white-space: nowrap; }
.sub-mark { color: var(--muted); }
.bar-col { width: 120px; min-width: 80px; padding-left: 0 !important; }
.bar { height: 8px; margin-top: 8px; background: var(--muted-bg); border-radius: 999px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--accent); border-radius: 999px; }
.filter { max-width: 320px; margin-bottom: 12px; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip { display: inline-flex; align-items: baseline; gap: 6px; padding: 1px 8px; border-radius: 999px; background: var(--muted-bg); font-size: 12px; }
.chip-count { color: var(--muted); font-variant-numeric: tabular-nums; }
details.more { margin-top: 4px; font-size: 12px; }
details.more summary { color: var(--accent); cursor: pointer; }
details.more .chips { margin-top: 4px; }
.muted-text { color: var(--muted); }
.small-hint { font-size: 12px; margin-top: 8px; }

/* 日々の推移 */
.history { margin-top: 32px; }
.filters { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px; margin-bottom: 12px; }
label.inline { display: inline-flex; align-items: center; gap: 6px; margin: 0; }
label.inline select { width: auto; max-width: 14em; margin: 0; padding: 4px 8px; }
.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; background: var(--surface); }
.segmented button { border: none; border-radius: 0; padding: 4px 12px; color: var(--muted); background: none; }
.segmented button + button { border-left: 1px solid var(--border); }
.segmented button.active { background: var(--muted-bg); color: var(--text); font-weight: 600; }
.chart-card { position: relative; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 16px 8px; }
.chart-title { font-weight: 600; margin-bottom: 4px; }
.chart { min-height: 260px; }
.chart.loading { opacity: .5; }
.chart svg { display: block; width: 100%; height: 260px; overflow: visible; }
.chart svg:focus { outline: none; }
.chart svg:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
.chart-empty { display: grid; place-items: center; height: 260px; color: var(--muted); text-align: center; padding: 0 16px; }
.axis-text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.grid-line { stroke: var(--border); stroke-width: 1; }
.base-line { stroke: var(--muted); stroke-width: 1; opacity: .6; }
.series-line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.series-dot { fill: var(--accent); stroke: var(--surface); stroke-width: 2; }
.series-bar { fill: var(--accent); }
.series-bar.dim { opacity: .45; }
.crosshair { stroke: var(--muted); stroke-width: 1; }
.chart-tooltip {
  position: absolute; pointer-events: none; z-index: 1; min-width: 120px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
  box-shadow: 0 4px 12px rgb(0 0 0 / .12); padding: 6px 10px; font-size: 12px;
}
.tooltip-date { color: var(--muted); }
.tooltip-row { display: flex; align-items: center; gap: 6px; }
.tooltip-key { width: 12px; height: 2px; background: var(--accent); border-radius: 1px; }
.tooltip-value { font-size: 14px; font-weight: 700; font-variant-numeric: tabular-nums; }
.tooltip-sub { color: var(--muted); font-variant-numeric: tabular-nums; }
details.chart-table { margin-top: 8px; }
details.chart-table summary { color: var(--accent); cursor: pointer; font-size: 13px; margin-bottom: 8px; }
details.chart-table .table-wrap { max-height: 320px; overflow-y: auto; }
#history-hint { margin-top: 12px; }
