:root {
  --bg: #0d0b0a;
  --bg-elevated: #161210;
  --bg-card: #1c1714;
  --border: #3a2f28;
  --text: #e8dfd4;
  --text-muted: #9a8b7a;
  --accent: #c45c2a;
  --accent-dim: #8b3d18;
  --gold: #c9a227;
  --hint: #5a7a9a;
  --narrative: #d4c4b0;
  --choice: #7eb87a;
  --condition: #b8a060;
  --token-energy: #4a9fd4;
  --token-health: #c44a4a;
  --token-terror: #6a4a8a;
  --token-stat: #c9a227;
  --radius: 8px;
  --font-serif: "Noto Serif SC", "Source Han Serif SC", "Songti SC", serif;
  --font-sans: "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  line-height: 1.6;
}

body {
  background-image:
    radial-gradient(ellipse 80% 50% at 50% -20%, rgba(196, 92, 42, 0.12), transparent),
    linear-gradient(180deg, var(--bg) 0%, #080706 100%);
}

.app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  max-width: 1400px;
  margin: 0 auto;
}

.header {
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: rgba(22, 18, 16, 0.9);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-inner {
  margin-bottom: 0.75rem;
}

.title {
  margin: 0;
  font-family: var(--font-serif);
  font-size: 1.75rem;
  font-weight: 600;
  color: var(--gold);
  letter-spacing: 0.08em;
}

.subtitle {
  margin: 0.25rem 0 0;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.search-wrap {
  position: relative;
}

.search-wrap input {
  width: 100%;
  padding: 0.65rem 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: var(--text);
  font-size: 0.95rem;
}

.search-wrap input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(196, 92, 42, 0.2);
}

.search-results {
  position: absolute;
  left: 0;
  right: 0;
  top: 100%;
  margin: 4px 0 0;
  padding: 0;
  list-style: none;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  max-height: 320px;
  overflow-y: auto;
  z-index: 200;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.5);
}

.search-results li button {
  width: 100%;
  padding: 0.6rem 1rem;
  border: none;
  background: transparent;
  color: var(--text);
  text-align: left;
  cursor: pointer;
  font-size: 0.9rem;
}

.search-results li button:hover {
  background: var(--bg-card);
}

.search-results .kind-tag {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-left: 0.5rem;
}

.main {
  display: flex;
  flex: 1;
  min-height: 0;
}

.sidebar {
  width: 280px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  background: var(--bg-elevated);
}

.sidebar-head {
  padding: 1rem;
  border-bottom: 1px solid var(--border);
}

.sidebar-head label {
  display: block;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
}

.loc-jump {
  display: flex;
  gap: 0.5rem;
}

.loc-jump input {
  flex: 1;
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: var(--text);
}

.loc-jump button {
  padding: 0.45rem 0.85rem;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  font-weight: 500;
}

.loc-jump button:hover {
  background: var(--accent-dim);
}

.filter-wrap {
  padding: 0.5rem 1rem;
}

.filter-wrap input {
  width: 100%;
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: var(--text);
  font-size: 0.85rem;
}

.loc-list {
  flex: 1;
  overflow-y: auto;
  margin: 0;
  padding: 0.25rem 0;
  list-style: none;
}

.loc-list li button {
  width: 100%;
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  border: none;
  background: transparent;
  color: var(--text);
  text-align: left;
  cursor: pointer;
  font-size: 0.88rem;
}

.loc-list li button:hover,
.loc-list li button.active {
  background: rgba(196, 92, 42, 0.15);
}

.loc-list .num {
  flex-shrink: 0;
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
  font-size: 0.8rem;
  min-width: 2.5rem;
}

.loc-list .icons {
  margin-left: auto;
  font-size: 0.65rem;
  color: var(--gold);
}

.content {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;
  min-width: 0;
}

.empty-state {
  text-align: center;
  padding: 4rem 2rem;
  color: var(--text-muted);
}

.location-panel.hidden,
.empty-state.hidden,
.hidden {
  display: none !important;
}

.loc-header {
  margin-bottom: 1rem;
}

.loc-badge {
  display: inline-block;
  padding: 0.2rem 0.5rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-right: 0.5rem;
}

.loc-header h2 {
  display: inline;
  margin: 0;
  font-family: var(--font-serif);
  font-size: 1.5rem;
  color: var(--gold);
}

.tabs {
  display: flex;
  gap: 0.25rem;
  margin-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}

.tab {
  padding: 0.6rem 1rem;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.9rem;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}

.tab:hover {
  color: var(--text);
}

.tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.tab-panel {
  display: none;
}

.tab-panel.active {
  display: block;
}

.verse-nav {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.verse-nav label {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.verse-nav select {
  padding: 0.4rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: var(--text);
  min-width: 120px;
}

.verse-count {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.verse-body {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.element {
  padding: 1rem 1.15rem;
  border-radius: var(--radius);
  border-left: 3px solid var(--border);
  background: var(--bg-card);
}

.element.hint {
  border-left-color: var(--hint);
  background: rgba(90, 122, 154, 0.08);
}

.element.narrative {
  border-left-color: var(--narrative);
}

.speak-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  margin-top: 0.65rem;
  min-height: 44px;
  min-width: 88px;
  padding: 0.5rem 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: rgba(196, 92, 42, 0.12);
  color: var(--accent);
  font-size: 0.9rem;
  font-family: var(--font-sans);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  transition: background 0.15s, border-color 0.15s;
}

.speak-btn:hover {
  background: rgba(196, 92, 42, 0.22);
  border-color: var(--accent);
}

.speak-btn.playing,
.speak-btn.status-speaking {
  background: rgba(74, 159, 212, 0.18);
  border-color: #4a9fd4;
  color: #9ed4f8;
}

.speak-btn.status-preparing,
.speak-btn.status-stopping {
  background: rgba(201, 162, 39, 0.15);
  border-color: var(--gold);
  color: var(--gold);
}

.speak-btn.status-error {
  background: rgba(196, 74, 74, 0.18);
  border-color: #c44a4a;
  color: #f0a0a0;
}

.speak-btn.status-done {
  background: rgba(126, 184, 122, 0.15);
  border-color: var(--choice);
  color: var(--choice);
}

.speak-icon {
  font-size: 0.85rem;
  line-height: 1;
  flex-shrink: 0;
}

.speak-label {
  font-variant-numeric: tabular-nums;
}

.element.condition {
  border-left-color: var(--condition);
  background: rgba(184, 160, 96, 0.06);
}

.element.choice {
  border-left-color: var(--choice);
}

.element.jumpable {
  cursor: pointer;
  transition: background 0.15s;
}

.element.choice.jumpable:hover {
  background: rgba(126, 184, 122, 0.1);
}

.element.condition.jumpable:hover,
.element.hint.jumpable:hover {
  background: rgba(184, 160, 96, 0.12);
}

.element-type {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
}

.element-text {
  font-family: var(--font-serif);
  font-size: 1.02rem;
  line-height: 1.75;
}

.element-text i {
  font-style: italic;
  color: var(--narrative);
}

.choice-target {
  margin-top: 0.5rem;
  font-size: 0.8rem;
  color: var(--choice);
}

.choice-result {
  margin-top: 0.35rem;
  font-size: 0.85rem;
  color: var(--text-muted);
  font-style: italic;
}

.rich-text {
  font-family: var(--font-serif);
  font-size: 1.05rem;
  line-height: 1.8;
  padding: 1rem;
  background: var(--bg-card);
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

.rich-text:empty::before,
.muted {
  color: var(--text-muted);
  font-style: italic;
}

.extra-block {
  margin-bottom: 1.5rem;
}

.extra-block h3 {
  margin: 0 0 0.5rem;
  font-size: 0.95rem;
  color: var(--accent);
}

.token {
  display: inline;
  padding: 0.1em 0.35em;
  border-radius: 3px;
  font-weight: 600;
  font-size: 0.9em;
  font-family: var(--font-sans);
  white-space: nowrap;
}

.token-energy { background: rgba(74, 159, 212, 0.25); color: #7ec8f0; }
.token-health { background: rgba(196, 74, 74, 0.25); color: #f0a0a0; }
.token-terror { background: rgba(106, 74, 138, 0.25); color: #c8a8e8; }
.token-courage { background: rgba(196, 120, 42, 0.25); color: #f0c878; }
.token-stat { background: rgba(201, 162, 39, 0.2); color: var(--gold); }
.token-encounter { background: rgba(126, 184, 122, 0.2); color: var(--choice); }

.footer {
  padding: 0.75rem 1.5rem;
  border-top: 1px solid var(--border);
  font-size: 0.75rem;
  color: var(--text-muted);
  text-align: center;
}

.speech-toast {
  position: fixed;
  left: 50%;
  bottom: calc(1rem + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%) translateY(120%);
  max-width: min(90vw, 360px);
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  background: rgba(28, 23, 20, 0.95);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 0.85rem;
  text-align: center;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: transform 0.25s ease, opacity 0.25s ease;
}

.speech-toast.visible {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

@media (max-width: 768px) {
  .main {
    flex-direction: column;
  }

  .sidebar {
    width: 100%;
    max-height: 40vh;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  .header {
    padding-top: calc(1rem + env(safe-area-inset-top, 0px));
  }

  .footer {
    padding-bottom: calc(0.75rem + env(safe-area-inset-bottom, 0px));
  }
}
