:root {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1f232c;
  --border: #2a2f3a;
  --text: #e8eaf0;
  --text-muted: #8b91a3;
  --accent: #6ee7b7;
  --danger: #f87171;

  --cat-manga: #6ee7b7;
  --cat-novel: #60a5fa;
  --cat-lit: #a78bfa;
  --cat-art: #f472b6;
  --cat-life: #facc15;
  --cat-album: #22d3ee;
  --cat-vinyl: #fb923c;
  --cat-figure: #f59e0b;

  --st-read: #4caf6d;
  --st-reading: #60a5fa;
  --st-unread: #8b91a3;
  --st-notowned: #6b7280;
  --st-unreleased: #a78bfa;
  --st-shipping: #fb923c;

  --radius: 12px;
  --radius-sm: 8px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 16px;
  --space-4: 24px;
  --space-5: 32px;
  font-synthesis: none;
}

[data-theme="light"] {
  --bg: #fafaf9;
  --surface: #ffffff;
  --surface-2: #f2f1ee;
  --border: #e5e3de;
  --text: #1c1a17;
  --text-muted: #6b6a66;
  --accent: #ea580c;
  --danger: #dc2626;

  --cat-manga: #059669;
  --cat-novel: #2563eb;
  --cat-lit: #7c3aed;
  --cat-art: #db2777;
  --cat-life: #ca8a04;
  --cat-album: #0891b2;
  --cat-vinyl: #ea580c;
  --cat-figure: #d97706;

  --st-read: #16a34a;
  --st-reading: #2563eb;
  --st-unread: #78716c;
  --st-notowned: #a8a29e;
  --st-unreleased: #7c3aed;
  --st-shipping: #ea580c;
}

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

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

a { color: inherit; text-decoration: none; }

.app {
  max-width: 1100px;
  margin: 0 auto;
  padding: var(--space-4) var(--space-3) 96px;
}

header.top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: var(--space-4);
}
header.top h1 { font-size: 20px; font-weight: 700; margin: 0; }

.theme-toggle {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  width: 34px;
  height: 34px;
  font-size: 16px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

nav.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom, 0);
  z-index: 10;
}
nav.tabbar a {
  flex: 1;
  text-align: center;
  padding: 10px 4px 8px;
  font-size: 11px;
  color: var(--text-muted);
}
nav.tabbar a .icon { display: block; font-size: 20px; margin-bottom: 2px; }
nav.tabbar a.active { color: var(--accent); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--space-3);
  margin-bottom: var(--space-3);
}
.card h2 {
  font-size: 15px;
  font-weight: 600;
  margin: 0 0 var(--space-2);
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: var(--accent);
  color: #0a1a13;
  border: none;
  border-radius: var(--radius-sm);
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
[data-theme="light"] .btn { color: #fff; }
.btn.secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn.danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn-row { display: flex; gap: var(--space-2); }
.btn.block { width: 100%; }

input, select, textarea {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 9px 10px;
  font-size: 14px;
  font-family: inherit;
  width: 100%;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }
textarea { resize: vertical; min-height: 60px; }

label { display: block; font-size: 12px; color: var(--text-muted); margin-bottom: var(--space-1); }
.field { margin-bottom: var(--space-3); }
.field-row { display: flex; gap: var(--space-2); }
.field-row .field { flex: 1; }

.brand-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: var(--space-3); }
.brand-logo-wrap { background: #fff; border-radius: 8px; padding: 5px 10px; display: inline-flex; align-items: center; }
.brand-logo { height: 18px; width: auto; display: block; }
.sub-brand { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-muted); font-weight: 600; }
.sub-brand-icon { height: 18px; width: 18px; border-radius: 5px; object-fit: cover; }

.stat-row { display: flex; gap: var(--space-3); margin-top: var(--space-3); flex-wrap: wrap; }
.stat { flex: 1; min-width: 70px; text-align: center; }
.stat .n { font-size: 22px; font-weight: 700; }
.stat .l { font-size: 11px; color: var(--text-muted); }

.muted { color: var(--text-muted); }
.empty-state { text-align: center; padding: var(--space-5) var(--space-3); color: var(--text-muted); }

/* Toolbar: search + filter chips */
.toolbar { margin-bottom: var(--space-3); }
.search-input { margin-bottom: var(--space-2); }
.chip-row { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 12.5px;
  cursor: pointer;
  white-space: nowrap;
}
.chip.active {
  border-color: var(--accent);
  color: #0a1a13;
  font-weight: 700;
  background: linear-gradient(135deg, color-mix(in srgb, var(--accent) 80%, white 20%), var(--accent));
  box-shadow: 0 2px 8px -2px color-mix(in srgb, var(--accent) 60%, transparent);
}
[data-theme="light"] .chip.active { color: #fff; }

/* Spreadsheet-style editable table */
.sheet-wrap {
  overflow: auto;
  max-height: 70vh;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: var(--space-3);
}
table.sheet {
  border-collapse: collapse;
  width: 100%;
  font-size: 13px;
}
table.sheet th, table.sheet td {
  border-bottom: 1px solid var(--border);
  padding: 0;
  white-space: nowrap;
  position: relative;
}
table.sheet th {
  position: sticky;
  top: 0;
  background: var(--surface-2);
  cursor: pointer;
  text-align: left;
  padding: 8px 10px;
  font-weight: 600;
  color: var(--text-muted);
  user-select: none;
  z-index: 10;
}
table.sheet th .sort-arrow { margin-left: 3px; color: var(--accent); }
table.sheet td:first-child, table.sheet th:first-child {
  position: sticky;
  left: 0;
  background: var(--surface);
  z-index: 4;
  min-width: 220px;
}
table.sheet th:first-child { background: var(--surface-2); z-index: 12; }
table.sheet tr:hover td { background: var(--surface-2); }
table.sheet tr:hover td:first-child { background: var(--surface-2); }
table.sheet input, table.sheet select {
  border: none;
  background: transparent;
  border-radius: 0;
  padding: 8px 10px;
  font-size: 13px;
  width: 100%;
  min-width: 100px;
  overflow: hidden;
  text-overflow: ellipsis;
}
/* Clicking a cell temporarily widens it to show/edit the full value,
   instead of permanently widening every column (which would make the
   already-wide sheet even harder to scan). Reverts on blur. */
table.sheet input:focus, table.sheet select:focus {
  position: absolute;
  inset: 0;
  width: max-content;
  min-width: 100%;
  max-width: 360px;
  background: var(--surface-2);
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  overflow: visible;
  text-overflow: initial;
  z-index: 6;
  box-shadow: 0 4px 14px rgba(0,0,0,0.45);
}
table.sheet td:first-child input:focus { z-index: 6; }
table.sheet .row-delete {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 8px 10px;
  font-size: 14px;
}
table.sheet .row-delete:hover { color: var(--danger); }
table.sheet tr.draft-row td:first-child { background: color-mix(in srgb, var(--accent) 8%, var(--surface)); }
table.sheet tr.draft-row:hover td:first-child { background: color-mix(in srgb, var(--accent) 8%, var(--surface-2)); }

.log-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 12px;
  padding: 4px 9px;
  white-space: nowrap;
}
.log-btn:hover { border-color: var(--accent); color: var(--accent); }
.log-btn.has-logs {
  color: var(--accent);
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  box-shadow: 0 0 8px -2px color-mix(in srgb, var(--accent) 70%, transparent);
}

.sheet-toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: var(--space-2); }

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
}
.modal-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: var(--space-4) var(--space-3);
  max-width: 640px;
  width: 100%;
  max-height: 88vh;
  overflow-y: auto;
}
.modal-box h2 { margin: 0 0 var(--space-3); font-size: 17px; text-transform: none; letter-spacing: normal; color: var(--text); }

.log-entry {
  border-bottom: 1px solid var(--border);
  padding: var(--space-2) 0;
}
.log-entry:last-child { border-bottom: none; }
.log-entry .log-date { font-size: 11px; color: var(--text-muted); margin-bottom: 2px; display:flex; justify-content:space-between; align-items:center; }
.log-entry .log-content { font-size: 13.5px; white-space: pre-wrap; }

.chip-delete {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 13px;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
  line-height: 1;
}
.chip-delete:hover { color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent); }

.confirm-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  z-index: 60;
}
.confirm-box { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: var(--space-4); max-width: 340px; width: 100%; }
.confirm-box p { white-space: pre-line; font-size: 14px; margin: 0 0 var(--space-4); }

/* Manga series progress */
.series-card { margin-bottom: var(--space-3); }
.series-card .row { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 6px; }
.series-card .name { font-weight: 600; font-size: 14.5px; }
.series-card .count { font-size: 12px; color: var(--text-muted); }
.progress-track { background: var(--surface-2); border-radius: 999px; height: 8px; overflow: hidden; }
.progress-fill { background: var(--cat-manga); height: 100%; border-radius: 999px; }

/* Bookshelf: each series is a "shelf" of colored spine blocks, one per volume */
.shelf-row {
  display: flex;
  align-items: flex-end;
  gap: 3px;
  flex-wrap: wrap;
  margin-top: var(--space-2);
  padding: 0 0 8px;
  border-bottom: 3px solid var(--border);
}
.spine {
  width: 28px;
  border-radius: 3px 3px 0 0;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: 4px;
  font-size: 10px;
  font-weight: 700;
  cursor: pointer;
  border: none;
  color: #fff;
}
.spine.st-已閱 { background: linear-gradient(160deg, color-mix(in srgb, var(--st-read) 85%, white 15%), var(--st-read)); box-shadow: 0 2px 8px -2px color-mix(in srgb, var(--st-read) 60%, transparent); }
.spine.st-閱讀中 { background: linear-gradient(160deg, color-mix(in srgb, var(--st-reading) 85%, white 15%), var(--st-reading)); box-shadow: 0 2px 8px -2px color-mix(in srgb, var(--st-reading) 60%, transparent); }
.spine.st-未讀 { background: linear-gradient(160deg, color-mix(in srgb, var(--text-muted) 90%, white 10%), var(--text-muted)); }
.spine.st-配送中 { background: linear-gradient(160deg, color-mix(in srgb, var(--st-shipping) 85%, white 15%), var(--st-shipping)); box-shadow: 0 2px 8px -2px color-mix(in srgb, var(--st-shipping) 60%, transparent); }
.spine.st-未擁有 {
  background: transparent;
  border: 1px dashed var(--st-notowned);
  color: var(--st-notowned);
}
.spine.st-未發行 {
  background: transparent;
  border: 1px dotted var(--st-unreleased);
  color: var(--st-unreleased);
  opacity: 0.6;
}
.spine.add-spine {
  background: transparent;
  border: 1px dashed var(--accent);
  color: var(--accent);
  font-size: 18px;
  height: 50px !important;
}
.spine:hover { filter: brightness(1.15); }
