/* === RESET & VARIABLES === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:           #0d1117;
  --surface:      #161b22;
  --surface-2:    #1c2128;
  --border:       #30363d;
  --text:         #e6edf3;
  --text-muted:   #8b949e;
  --text-subtle:  #6e7681;
  --accent:       #58a6ff;
  --accent-rgb:   88, 166, 255;
  --mark-bg:      rgba(88, 166, 255, 0.2);
  --mark-color:   #79c0ff;
  --header-h:     54px;
  --sidebar-w:    220px;
  --r:            8px;
  --r-sm:         5px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg:          #f6f8fa;
    --surface:     #ffffff;
    --surface-2:   #f0f3f6;
    --border:      #d0d7de;
    --text:        #1f2328;
    --text-muted:  #57606a;
    --text-subtle: #6e7781;
    --mark-bg:     rgba(88, 166, 255, 0.18);
    --mark-color:  #0550ae;
  }
}

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  overflow: hidden;
}

/* === HEADER === */
#header {
  position: fixed;
  inset: 0 0 auto 0;
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  z-index: 200;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.brand-icon {
  width: 28px;
  height: 28px;
  background: var(--accent);
  color: #000;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.05em;
}

.brand-text {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
}

.search-wrap {
  flex: 1;
  position: relative;
  max-width: 420px;
}

.search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-subtle);
  pointer-events: none;
}

#search-input {
  width: 100%;
  padding: 7px 12px 7px 32px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--r);
  color: var(--text);
  font-size: 13px;
  outline: none;
  transition: border-color 0.15s;
}

#search-input:focus { border-color: var(--accent); }
#search-input::placeholder { color: var(--text-subtle); }

.dir-btn {
  flex-shrink: 0;
  padding: 5px 12px;
  background: rgba(var(--accent-rgb), 0.12);
  color: var(--accent);
  border: 1px solid rgba(var(--accent-rgb), 0.35);
  border-radius: var(--r-sm);
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  letter-spacing: 0.03em;
  transition: background 0.15s;
  white-space: nowrap;
}

.dir-btn:hover { background: rgba(var(--accent-rgb), 0.22); }

.back-btn {
  flex-shrink: 0;
  padding: 5px 12px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
  transition: border-color 0.15s, color 0.15s;
}

.back-btn:hover { border-color: var(--accent); color: var(--accent); }

/* === LAYOUT === */
#app { height: 100vh; overflow: hidden; }

.layout {
  display: flex;
  height: calc(100vh - var(--header-h));
  margin-top: var(--header-h);
}

/* === SIDEBAR === */
#sidebar {
  width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  flex-shrink: 0;
  padding: 14px 0 20px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.sidebar-label {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.1em;
  color: var(--text-subtle);
  padding: 4px 16px 6px;
  text-transform: uppercase;
  margin-top: 4px;
}

#chapter-nav { margin-bottom: 4px; }

.chapter-btn {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  padding: 7px 16px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 13px;
  text-align: left;
  cursor: pointer;
  transition: color 0.12s, background 0.12s;
}

.chapter-btn:hover {
  background: var(--surface-2);
  color: var(--text);
}

.chapter-btn.active {
  color: var(--accent);
  background: rgba(var(--accent-rgb), 0.08);
  font-weight: 500;
}

.chapter-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--border);
  flex-shrink: 0;
  transition: background 0.12s;
}

.chapter-btn.active .chapter-dot { background: var(--accent); }

/* === SECTION FILTERS === */
#section-filters {
  padding: 0 10px 8px;
}

#section-filters .sidebar-label { padding-left: 6px; }

.filter-pill {
  display: block;
  width: 100%;
  text-align: left;
  padding: 5px 10px;
  margin-bottom: 3px;
  background: none;
  border: 1px solid var(--border);
  border-radius: 20px;
  color: var(--text-muted);
  font-size: 11px;
  cursor: pointer;
  transition: all 0.12s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.filter-pill:hover {
  border-color: rgba(var(--accent-rgb), 0.5);
  color: var(--text);
}

.filter-pill.active {
  background: rgba(var(--accent-rgb), 0.14);
  border-color: rgba(var(--accent-rgb), 0.6);
  color: var(--accent);
  font-weight: 600;
}

/* === MAIN CONTENT === */
#main-content {
  flex: 1;
  overflow-y: auto;
  padding: 24px 28px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.chapter-top {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 28px;
}

.chapter-title {
  font-size: 21px;
  font-weight: 700;
  color: var(--accent);
}

.chapter-count {
  font-size: 12px;
  color: var(--text-subtle);
}

/* === SECTION GROUPS === */
.section-group { margin-bottom: 36px; }

.section-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-bottom: 9px;
  margin-bottom: 12px;
  border-bottom: 1px solid var(--border);
}

.section-name {
  font-size: 12px;
  font-weight: 700;
  color: var(--text);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.section-page {
  font-size: 11px;
  color: var(--text-subtle);
}

/* === CARDS GRID === */
.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(265px, 1fr));
  gap: 8px;
}

/* === WORD CARD === */
.word-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r);
  overflow: hidden;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.word-card:hover {
  border-color: rgba(var(--accent-rgb), 0.45);
  box-shadow: 0 0 0 3px rgba(var(--accent-rgb), 0.06);
}

.card-main {
  display: flex;
  align-items: flex-start;
  gap: 6px;
  padding: 11px 12px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}

.card-body { flex: 1; min-width: 0; }

.card-primary {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 2px;
  word-break: break-word;
}

.card-secondary {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 7px;
  word-break: break-word;
}

.card-foot {
  display: flex;
  align-items: center;
  gap: 5px;
  flex-wrap: wrap;
}

/* Section badge — 6 cycling colours */
.section-badge {
  font-size: 9px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  white-space: nowrap;
}

.section-badge[data-idx="0"] { background: hsl(213 80% 55% / .16); color: hsl(213 80% 65%); }
.section-badge[data-idx="1"] { background: hsl(140 55% 50% / .16); color: hsl(140 55% 58%); }
.section-badge[data-idx="2"] { background: hsl(280 65% 60% / .16); color: hsl(280 65% 70%); }
.section-badge[data-idx="3"] { background: hsl(35 90% 55% / .16);  color: hsl(35 90% 60%); }
.section-badge[data-idx="4"] { background: hsl(185 65% 50% / .16); color: hsl(185 65% 57%); }
.section-badge[data-idx="5"] { background: hsl(345 70% 58% / .16); color: hsl(345 70% 65%); }

.page-ref {
  font-size: 10px;
  color: var(--text-subtle);
}

.chevron {
  font-size: 16px;
  line-height: 1;
  color: var(--text-subtle);
  flex-shrink: 0;
  margin-top: 2px;
  transition: transform 0.2s ease;
}

.word-card.open .chevron { transform: rotate(90deg); }

/* === EXPANDED CONTENT === */
.card-expanded {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.25s ease;
}

.card-exp-inner {
  padding: 10px 12px 12px;
  border-top: 1px solid var(--border);
}

.expl {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 6px;
  line-height: 1.6;
}

.example {
  font-size: 11px;
  color: var(--text-subtle);
  font-style: italic;
  line-height: 1.6;
}

/* === SEARCH HIGHLIGHT === */
mark {
  background: var(--mark-bg);
  color: var(--mark-color);
  border-radius: 2px;
  padding: 0 1px;
  font-style: inherit;
}

/* === STATES === */
.loading, .no-results {
  text-align: center;
  color: var(--text-muted);
  padding: 60px 0 40px;
  font-size: 14px;
}

.no-results { padding-top: 40px; }

/* === SCROLLBARS (Webkit) === */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-subtle); }

/* === MOBILE === */
@media (max-width: 640px) {
  body { overflow: auto; }
  #app { height: auto; overflow: visible; }

  .layout {
    flex-direction: column;
    height: auto;
    margin-top: var(--header-h);
  }

  #sidebar {
    width: 100%;
    height: auto;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 8px 0 4px;
    overflow-x: auto;
    overflow-y: hidden;
  }

  #chapter-nav {
    display: flex;
    flex-direction: row;
    padding: 0 8px;
    gap: 4px;
    overflow-x: auto;
  }

  .chapter-btn {
    white-space: nowrap;
    padding: 5px 12px;
    border-radius: 20px;
    border: 1px solid var(--border);
    width: auto;
    flex-shrink: 0;
  }

  .chapter-btn.active { border-color: rgba(var(--accent-rgb), 0.5); }
  .chapter-dot { display: none; }

  #section-filters {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    gap: 4px;
    padding: 4px 8px 6px;
  }

  #section-filters .sidebar-label { display: none; }

  .filter-pill {
    display: inline-block;
    width: auto;
    flex-shrink: 0;
    margin-bottom: 0;
  }

  #main-content { padding: 16px; }

  .brand-text { display: none; }

  .cards-grid { grid-template-columns: 1fr; }
}

.site-footer {
  text-align: center;
  font-size: 11px;
  color: var(--text-subtle);
  padding: 16px 0 20px;
  letter-spacing: 0.04em;
}
