:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #eef1f6;
  --border: #e2e6ee;
  --border-strong: #cfd5e0;
  --text: #171a20;
  --muted: #6a7280;
  --accent: #3d63dd;
  --accent-soft: #e9eefd;
  --danger: #c0392b;
  --danger-soft: #fdeceb;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow-sm: 0 1px 2px rgb(16 24 40 / .06);
  --shadow-md: 0 6px 20px rgb(16 24 40 / .10);
  --topbar-h: 64px;
  --gutter: 24px;
  --maxw: 1200px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121419;
    --surface: #1b1e24;
    --surface-2: #23272f;
    --border: #2d323b;
    --border-strong: #3c424c;
    --text: #e6e8ec;
    --muted: #949ca8;
    --accent: #7c9cff;
    --accent-soft: #212a44;
    --danger: #ef6c5a;
    --danger-soft: #3a221f;
    --shadow-sm: 0 1px 2px rgb(0 0 0 / .3);
    --shadow-md: 0 6px 20px rgb(0 0 0 / .38);
  }
}

* { box-sizing: border-box; }

/* Author `display` rules outrank the UA `[hidden]` rule, so state that wins. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 system-ui, -apple-system, "Noto Sans TC", "PingFang TC",
        "Microsoft JhengHei", sans-serif;
  -webkit-font-smoothing: antialiased;
}

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

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

button {
  cursor: pointer;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 7px 13px;
  transition: background-color .12s, border-color .12s, color .12s;
}

button:hover { border-color: var(--accent); color: var(--accent); }
button:active { transform: translateY(1px); }

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 500;
  box-shadow: var(--shadow-sm);
}

button.primary:hover { filter: brightness(1.08); color: #fff; }

input, select {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 8px 11px;
  transition: border-color .12s, box-shadow .12s;
}

input:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

input::placeholder { color: var(--muted); opacity: .8; }

/* ---------------------------------------------------------------- topbar */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface); /* fallback before color-mix */
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(10px);
  backdrop-filter: saturate(180%) blur(10px);
  border-bottom: 1px solid var(--border);
}

/* Keeps the brand and the doc list on the same left edge at any width. */
.topbar-inner {
  max-width: var(--maxw);
  min-height: var(--topbar-h);
  margin: 0 auto;
  padding: 12px var(--gutter);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand {
  margin: 0;
  font-size: 18px;
  font-weight: 650;
  letter-spacing: .01em;
  white-space: nowrap;
}

.topbar-actions { display: flex; align-items: center; gap: 10px; }

.search-field {
  position: relative;
  display: flex;
  align-items: center;
}

.search-field svg {
  position: absolute;
  left: 10px;
  width: 15px;
  height: 15px;
  color: var(--muted);
  pointer-events: none;
}

.search-field input {
  width: 260px;
  padding-left: 32px;
  border-radius: 999px;
  background: var(--surface-2);
}

.search-field input:focus { background: var(--surface); }

/* ---------------------------------------------------------------- layout */

.layout {
  display: grid;
  grid-template-columns: 232px minmax(0, 1fr);
  gap: 36px;
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 28px var(--gutter) 64px;
  align-items: start;
}

.sidebar {
  position: sticky;
  top: calc(var(--topbar-h) + 20px);
}

.sidebar section + section {
  margin-top: 24px;
  padding-top: 24px;
  border-top: 1px solid var(--border);
}

.sidebar h2 {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .09em;
  color: var(--muted);
  margin: 0 0 10px;
}

/* --------------------------------------------------------- category list */

.filter-list { list-style: none; margin: 0 0 12px; padding: 0; }

.filter-list li {
  display: flex;
  align-items: center;
  gap: 2px;
  border-radius: var(--radius-sm);
  padding: 1px 4px 1px 10px;
}

.filter-list li:hover { background: var(--surface-2); }
.filter-list li.active { background: var(--accent-soft); }

.filter-list .name {
  flex: 1;
  background: none;
  border: none;
  padding: 6px 0;
  text-align: left;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.filter-list .name:hover { color: var(--accent); }
.filter-list li.active .name { color: var(--accent); font-weight: 600; }

.filter-list .count {
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  padding: 0 4px;
}

.filter-list .icon {
  background: none;
  border: none;
  padding: 3px 5px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1;
}

.filter-list .icon:hover { color: var(--accent); }
.filter-list .icon.del:hover { color: var(--danger); }

/* Reveal row controls on hover, but keep them permanent on touch devices. */
@media (hover: hover) {
  .filter-list .icon { opacity: 0; }
  .filter-list li:hover .icon,
  .filter-list li:focus-within .icon { opacity: 1; }
}

.inline-form { display: flex; gap: 6px; }
.inline-form input { flex: 1; min-width: 0; padding: 6px 10px; font-size: 13.5px; }
.inline-form button { padding: 6px 11px; }

/* ------------------------------------------------------------ tag cloud */

.tag-cloud { display: flex; flex-wrap: wrap; gap: 6px; }

.tag {
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--muted);
}

.tag:hover { border-color: var(--accent); color: var(--accent); background: var(--surface); }
.tag.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.tag.active:hover { color: #fff; }
.tag .count { opacity: .65; margin-left: 5px; font-size: 11px; }

.tag-cloud > .count { color: var(--muted); font-size: 13px; }

/* ------------------------------------------------------------- doc list */

.active-filters {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
  padding: 9px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  font-size: 13px;
}

.active-filters button {
  padding: 3px 10px;
  font-size: 12.5px;
  border-radius: 999px;
  background: var(--surface-2);
  border-color: transparent;
}

.doc-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }

.doc {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 7px 14px;
  box-shadow: var(--shadow-sm);
  transition: border-color .14s, box-shadow .14s, transform .14s;
}

.doc:hover {
  border-color: var(--border-strong);
  box-shadow: var(--shadow-md);
  transform: translateY(-1px);
}

.doc-title {
  grid-column: 1;
  align-self: center;
  font-size: 15.5px;
  font-weight: 600;
  line-height: 1.45;
  color: var(--text);
  text-decoration: none;
  overflow-wrap: anywhere;
}

.doc-title:hover { color: var(--accent); text-decoration: underline; text-underline-offset: 3px; }

.doc-meta {
  grid-column: 1;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 7px;
  font-size: 12.5px;
  color: var(--muted);
}

.doc-host {
  overflow-wrap: anywhere;
  padding-right: 3px;
}

.doc-meta .chip, .doc-meta .tag { font-size: 11.5px; padding: 2px 9px; }

.chip {
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: 999px;
  border: none;
  font-weight: 500;
}

.doc-actions {
  grid-column: 2;
  grid-row: 1 / span 2;
  display: flex;
  align-items: center;
  gap: 6px;
}

.doc-actions button { padding: 5px 11px; font-size: 12.5px; }
.doc-actions .del:hover { border-color: var(--danger); color: var(--danger); background: var(--danger-soft); }

@media (hover: hover) {
  .doc-actions { opacity: 0; transition: opacity .14s; }
  .doc:hover .doc-actions,
  .doc:focus-within .doc-actions { opacity: 1; }
}

.empty {
  color: var(--muted);
  text-align: center;
  padding: 72px 24px;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
}

/* ---------------------------------------------------------------- dialog */

dialog {
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  padding: 24px;
  width: min(460px, calc(100vw - 32px));
  box-shadow: var(--shadow-md);
}

dialog::backdrop { background: rgb(12 16 24 / .5); backdrop-filter: blur(2px); }

dialog h2 { margin: 0 0 18px; font-size: 17px; }

dialog form { display: flex; flex-direction: column; }

dialog label {
  font-size: 12.5px;
  font-weight: 500;
  color: var(--muted);
  margin-bottom: 6px;
}

dialog label .hint { margin-left: 6px; font-weight: 400; opacity: .8; }

dialog input, dialog select { margin-bottom: 16px; }

dialog menu {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin: 8px 0 0;
  padding: 0;
}

dialog menu button { padding: 8px 16px; }

.form-error, .gate-error {
  color: var(--danger);
  background: var(--danger-soft);
  border-radius: var(--radius-sm);
  padding: 8px 11px;
  font-size: 13px;
  margin: 0 0 14px;
}

/* ------------------------------------------------------------------ gate */

.gate {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.gate-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow-md);
  padding: 30px;
  width: min(360px, 100%);
  display: flex;
  flex-direction: column;
}

.gate-card h1 { margin: 0 0 6px; font-size: 21px; }
.gate-card p { margin: 0 0 18px; color: var(--muted); font-size: 13.5px; }
.gate-card input { margin-bottom: 14px; }
.gate-card button { padding: 9px; }

/* ------------------------------------------------------------ responsive */

@media (max-width: 860px) {
  :root { --gutter: 16px; }

  .layout {
    grid-template-columns: 1fr;
    gap: 26px;
    padding-top: 20px;
  }

  .sidebar {
    position: static;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
  }

  .topbar-actions { width: 100%; }
  .search-field { flex: 1; }
  .search-field input { width: 100%; }
  #add-doc { white-space: nowrap; }
}

@media (max-width: 520px) {
  .doc { grid-template-columns: minmax(0, 1fr); }
  .doc-actions { grid-column: 1; grid-row: auto; opacity: 1; }
}
