:root {
  --bg: #f4f5f8;
  --panel: #ffffff;
  --text: #1b1f2a;
  --muted: #69707f;
  --line: #e1e4ea;
  --accent: #c28f00;
  --accent-text: #ffffff;
  --danger: #c8323b;
  --hover: #f0f2f6;
  --c-light: #e8c100;
  --c-water: #2f7fe0;
  --c-dark: #5a4a6e;
  --c-fire: #e0412f;
  --c-nature: #2e9d4c;
  --c-zero: #9aa0aa;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151c;
    --panel: #1b1f2a;
    --text: #e8eaf0;
    --muted: #9298a6;
    --line: #2c3240;
    --accent: #e0aa1a;
    --accent-text: #1b1f2a;
    --danger: #ff5c63;
    --hover: #232837;
    --c-dark: #8b78a6;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Hiragino Sans", "Yu Gothic UI", "Meiryo", sans-serif;
}
[hidden] { display: none !important; }
button, input { font: inherit; color: inherit; }
input[type="search"], input[type="number"], input:not([type]), input[type="text"], input[type="password"] {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  outline: none;
}
input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
button { cursor: pointer; border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 8px 14px; }
button:hover { background: var(--hover); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
button.primary:hover { filter: brightness(1.05); }
button.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }
button.ghost { background: transparent; }
button.link { background: none; border: none; color: var(--muted); text-decoration: underline; padding: 4px; }
button.wide { width: 100%; }
.spacer { flex: 1; }
.error { color: var(--danger); min-height: 1.2em; margin: 6px 0; font-size: 14px; }
.hint { color: var(--muted); font-size: 13px; }

.logo { font-size: 28px; margin: 0; letter-spacing: .02em; }
.logo span { color: var(--accent); }
.logo.small { font-size: 18px; }

/* ログイン */
.auth { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.auth-box { width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 24px; }
.auth-box .sub { margin: 2px 0 18px; color: var(--muted); }
.tabs { display: flex; margin-bottom: 14px; border-bottom: 1px solid var(--line); }
.tab { flex: 1; border: none; border-radius: 0; background: none; padding: 8px; color: var(--muted); border-bottom: 2px solid transparent; }
.tab.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
form label, .field { display: block; margin-bottom: 12px; font-size: 13px; color: var(--muted); }
form label input, .field .stepper { margin-top: 4px; }
form label input { color: var(--text); font-size: 15px; }

/* ヘッダー */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.menu {
  position: absolute; right: 12px; top: calc(100% + 4px);
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 8px 24px rgb(0 0 0 / .15); padding: 6px; min-width: 180px;
  display: flex; flex-direction: column;
}
.menu button { border: none; text-align: left; background: none; }
.menu button:hover { background: var(--hover); }

/* 検索・絞り込み */
.controls { max-width: 900px; margin: 0 auto; padding: 12px 16px 0; }
.search-row { display: flex; gap: 8px; }
.search-row button { white-space: nowrap; }
.civ-filter, .civ-pick, .type-filter, .type-pick { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.type-filter { margin-top: 6px; }
.type-filter .chip, .type-pick .chip { font-size: 13px; padding: 3px 10px; }
.chip {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 5px 11px; border-radius: 999px; font-size: 14px;
  border: 1px solid var(--line); background: var(--panel);
}
.chip .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--c); }
.chip[aria-pressed="true"] { border-color: var(--c, var(--accent)); background: color-mix(in srgb, var(--c, var(--accent)) 18%, var(--panel)); font-weight: 600; }
.filter-row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 14px; margin-top: 10px; font-size: 14px; color: var(--muted); }
.inline { display: inline-flex; align-items: center; gap: 4px; }
.inline input[type="number"] { width: 64px; padding: 5px 8px; }
.check input { margin: 0; }

.summary { max-width: 900px; margin: 10px auto 6px; padding: 0 16px; color: var(--muted); font-size: 13px; }

/* 一覧 */
.list { max-width: 900px; margin: 0 auto 40px; background: var(--panel); border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }
@media (min-width: 932px) { .list { border: 1px solid var(--line); border-radius: 12px; overflow: hidden; } }
.row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 150px 64px 128px;
  align-items: center;
  border-bottom: 1px solid var(--line);
  min-height: 48px;
}
#rows .row:last-child { border-bottom: none; }
.row.head {
  position: sticky; top: 49px; z-index: 5;
  background: var(--panel); font-size: 13px; color: var(--muted); min-height: 38px;
}
.row.head .col { border: none; border-radius: 0; background: none; text-align: left; padding: 8px 12px; color: inherit; font-weight: 600; }
.row.head .col:hover { background: var(--hover); }
.row.head .col[data-dir]::after { content: " ▲"; color: var(--accent); }
.row.head .col[data-dir="desc"]::after { content: " ▼"; }
.row.head .col { white-space: nowrap; }
.row.head .cost, .row.head .count { text-align: center !important; padding-left: 2px; padding-right: 2px; }
.row.zero { opacity: .45; }
.cell { padding: 6px 12px; }
.cell.name { cursor: pointer; overflow: hidden; }
.cell.name:hover .n { text-decoration: underline; }
.cell.name .n { display: block; word-break: break-all; }
.cell.name .meta { display: block; font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cell.name .type { display: inline-block; padding: 0 5px; margin-right: 4px; border: 1px solid var(--line); border-radius: 4px; }
.cell.cost { text-align: center; font-variant-numeric: tabular-nums; font-weight: 600; }
.cell.count { display: flex; align-items: center; justify-content: center; gap: 4px; }
.cell.count .num { min-width: 30px; text-align: center; font-weight: 700; font-variant-numeric: tabular-nums; }
.cell.count button { width: 32px; height: 32px; padding: 0; border-radius: 50%; line-height: 1; }
.civs { display: flex; flex-wrap: wrap; gap: 3px; }
.civb {
  display: inline-block; padding: 1px 7px; border-radius: 4px; font-size: 12px; font-weight: 600;
  color: #fff; background: var(--c);
}
.civb.light { color: #3a3000; }
.empty { text-align: center; color: var(--muted); padding: 32px 16px; margin: 0; }

@media (max-width: 600px) {
  .row { grid-template-columns: minmax(0, 1fr) 62px 40px 108px; }
  .cell, .row.head .col { padding-left: 8px; padding-right: 8px; }
  .row.head .col { padding: 8px 6px; }
  .civb { padding: 1px 4px; }
  .civb .full { display: none; }
  .cell.count button { width: 30px; height: 30px; }
}
@media (min-width: 601px) { .civb .short { display: none; } }

/* ダイアログ */
dialog {
  border: 1px solid var(--line); border-radius: 14px; padding: 0;
  background: var(--panel); color: var(--text);
  width: min(460px, calc(100vw - 32px));
}
dialog::backdrop { background: rgb(0 0 0 / .45); }
dialog form { padding: 20px; }
dialog h2 { margin: 0 0 14px; font-size: 18px; }
fieldset { border: none; padding: 0; margin: 0 0 12px; }
legend { font-size: 13px; color: var(--muted); padding: 0; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.stepper { display: flex; gap: 6px; }
.stepper input { text-align: center; }
.stepper button { padding: 0 12px; font-size: 18px; }
.actions { display: flex; gap: 8px; margin-top: 8px; }
.actions.column { flex-direction: column; }
.added { margin: 0 0 6px; font-size: 13px; color: var(--c-nature); min-height: 0; }
.added:empty { display: none; }
.actions .keep { margin: 0; font-size: 14px; color: var(--text); }
.links { font-size: 13px; margin: 0; }
.links a { color: var(--accent); margin-right: 12px; }
code { background: var(--hover); padding: 1px 5px; border-radius: 4px; }

.toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: var(--text); color: var(--panel); padding: 9px 16px; border-radius: 999px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: .2s; z-index: 100;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
