/* ==========================================================================
   CivNode — Base Stylesheet
   Reset, typography, layout foundation, custom properties
   ========================================================================== */

/* --- Self-hosted Fonts (see fonts.css) --- */

/* ==========================================================================
   Reset
   ========================================================================== */

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

ul, ol {
  list-style: none;
}

img, picture, video, canvas, svg {
  display: block;
  max-width: 100%;
}

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

input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  width: 1.1em;
  height: 1.1em;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-secondary);
  vertical-align: middle;
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition);
  position: relative;
}

input[type="checkbox"]:checked {
  background: var(--accent);
  border-color: var(--accent);
}

input[type="checkbox"]:checked::after {
  content: '';
  position: absolute;
  top: 0.05em;
  left: 0.25em;
  width: 0.35em;
  height: 0.6em;
  border: solid var(--bg-primary);
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

input[type="checkbox"]:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--accent-subtle);
}

input[type="number"] {
  -moz-appearance: textfield;
  appearance: textfield;
}
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

button {
  cursor: pointer;
  background: none;
  border: none;
}

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

table {
  border-collapse: collapse;
}

/* ==========================================================================
   Custom Properties — Midnight Theme (Default)
   ========================================================================== */

:root {
  /* Backgrounds */
  --bg-primary: #08080a;
  --bg-secondary: #0f0f14;
  --bg-surface: #16161e;
  --bg-elevated: #1e1e28;

  /* Text */
  --text-primary: #d4d0c8;
  --text-secondary: #8a8680;
  --text-muted: #5a5650;

  /* Accent */
  --accent: #c9a84c;
  --accent-hover: #dab85c;
  --accent-subtle: rgba(201, 168, 76, 0.08);

  /* Borders & Shadows */
  --border: #1e1e28;
  --border-subtle: #14141c;
  --shadow: rgba(0, 0, 0, 0.4);
  --shadow-xs: 0 1px 2px rgba(0,0,0,0.2);
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.25);
  --shadow-md: 0 2px 8px rgba(0,0,0,0.3);
  --shadow-lg: 0 4px 16px rgba(0,0,0,0.35);
  --shadow-xl: 0 8px 32px rgba(0,0,0,0.4);

  /* Icon filters — theme-controlled so icons adapt to backgrounds */
  --icon-filter: invert(1) brightness(0.75);       /* light icons on dark bg */
  --icon-filter-on-accent: none;                    /* dark icons on accent bg */

  /* Semantic */
  --resonance: #d4a574;
  --patron-gold: #c9a84c;
  --error: #cc4444;
  --success: #4a8a5a;
  --plot: #a855f7;
  --research: #3b82f6;
  --attention: #eab308;

  /* Typography */
  --font-serif: 'Literata', Georgia, 'Times New Roman', serif;
  --font-sans: 'Manrope', system-ui, -apple-system, sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', monospace;
  --font-tree: 'Inter', system-ui, -apple-system, sans-serif;

  /* Reading defaults (user-configurable via Settings) */
  --reading-size: 1.125rem;
  --reading-line-height: 1.6;
  --reading-para-spacing: 1em;

  /* Spacing & Sizing */
  --radius-sm: 3px;
  --radius: 6px;
  --radius-lg: 12px;
  --transition: 180ms ease;
  --content-width: 720px;
  --reading-width: 65ch;
}

/* ==========================================================================
   Base Typography & Body
   ========================================================================== */

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  scroll-behavior: smooth;
}

body {
  font-family: var(--font-sans);
  font-size: 1rem;
  line-height: 1.6;
  color: var(--text-primary);
  background-color: var(--bg-primary);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  min-height: 100vh;
  transition:
    background-color var(--transition),
    color var(--transition);
}

/* --- Headings --- */

h1, h2, h3, h4, h5, h6 {
  font-family: var(--font-serif);
  font-weight: 600;
  line-height: 1.25;
  color: var(--text-primary);
  letter-spacing: -0.01em;
}

h1 {
  font-size: 2.4rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

h2 {
  font-size: 1.8rem;
}

h3 {
  font-size: 1.4rem;
}

h4 {
  font-size: 1.15rem;
}

h5 {
  font-size: 1rem;
  font-weight: 600;
}

h6 {
  font-size: 0.875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
}

/* --- Body / Reading Text --- */

.reading,
.reading p,
article p,
article li {
  font-family: var(--font-serif);
  font-size: var(--reading-size);
  line-height: var(--reading-line-height);
  color: var(--text-primary);
}

.reading {
  max-width: var(--reading-width);
}

.reading p + p {
  margin-top: var(--reading-para-spacing);
}

.reading h1 { margin-top: 2em; margin-bottom: 0.5em; }
.reading h2 { font-size: 1.5rem; font-weight: 600; margin-top: 1.5em; margin-bottom: 0.5em; }
.reading h3 { font-size: 1.25rem; font-weight: 600; margin-top: 1.2em; margin-bottom: 0.4em; }

.reading blockquote {
  margin: 1.5em 0;
  padding: 0.8em 1.2em;
  border-left: 3px solid var(--accent);
  background: var(--accent-subtle);
  color: var(--text-secondary);
  font-style: italic;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

.reading blockquote p {
  color: var(--text-secondary);
}

.reading ul,
.reading ol {
  margin: 1em 0;
  padding-left: 1.5em;
}

.reading ul {
  list-style: disc;
}

.reading ol {
  list-style: decimal;
}

.reading li + li {
  margin-top: 0.4em;
}

.reading hr {
  border: none;
  height: 1px;
  background: var(--border);
  margin: 2.5em 0;
}

.reading img {
  border-radius: var(--radius);
  margin: 1.5em 0;
}

/* --- Inline Elements --- */

strong, b {
  font-weight: 700;
}

em, i {
  font-style: italic;
}

small {
  font-size: 0.85em;
  color: var(--text-secondary);
}

code {
  font-family: var(--font-mono);
  font-size: 0.875em;
  background: var(--bg-secondary);
  padding: 0.15em 0.4em;
  border-radius: var(--radius-sm);
  color: var(--accent);
}

pre {
  font-family: var(--font-mono);
  font-size: 0.85rem;
  line-height: 1.6;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.2em 1.4em;
  overflow-x: auto;
  margin: 1.5em 0;
}

pre code {
  background: none;
  padding: 0;
  border-radius: 0;
  color: var(--text-primary);
  font-size: inherit;
}

/* --- Links --- */

a {
  color: var(--accent);
  transition: color var(--transition);
}

a:hover {
  color: var(--accent-hover);
}

a:active {
  opacity: 0.8;
}

.reading a {
  text-decoration: underline;
  text-decoration-color: rgba(201, 168, 76, 0.3);
  text-underline-offset: 0.15em;
  transition:
    color var(--transition),
    text-decoration-color var(--transition);
}

.reading a:hover {
  color: var(--accent-hover);
  text-decoration-color: var(--accent-hover);
}

/* ==========================================================================
   Layout
   ========================================================================== */

.container {
  max-width: var(--content-width);
  margin-left: auto;
  margin-right: auto;
  padding-left: 1.5rem;
  padding-right: 1.5rem;
}

.container-narrow {
  max-width: 480px;
  margin-left: auto;
  margin-right: auto;
  padding-left: 1.5rem;
  padding-right: 1.5rem;
}

.container-wide {
  max-width: 960px;
  margin-left: auto;
  margin-right: auto;
  padding-left: 1.5rem;
  padding-right: 1.5rem;
}

/* ==========================================================================
   Selection
   ========================================================================== */

::selection {
  background-color: var(--accent);
  color: var(--bg-primary);
}

::-moz-selection {
  background-color: var(--accent);
  color: var(--bg-primary);
}

/* ==========================================================================
   Scrollbar
   ========================================================================== */

::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: var(--bg-primary);
}

::-webkit-scrollbar-thumb {
  background: var(--bg-elevated);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* Firefox */
* {
  scrollbar-width: thin;
  scrollbar-color: var(--bg-elevated) var(--bg-primary);
}

/* ==========================================================================
   Focus
   ========================================================================== */

:focus {
  outline: none;
}

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

/* ==========================================================================
   Reduced Motion
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ==========================================================================
   Utility Classes
   ========================================================================== */

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.text-serif { font-family: var(--font-serif); }
.text-sans  { font-family: var(--font-sans); }
.text-mono  { font-family: var(--font-mono); }

.text-primary   { color: var(--text-primary); }
.text-secondary { color: var(--text-secondary); }
.text-muted     { color: var(--text-muted); }
.text-accent    { color: var(--accent); }

.text-sm   { font-size: 0.85rem; }
.text-base { font-size: 1rem; }
.text-lg   { font-size: 1.125rem; }
.text-xl   { font-size: 1.4rem; }

.font-normal  { font-weight: 400; }
.font-medium  { font-weight: 500; }
.font-semi    { font-weight: 600; }
.font-bold    { font-weight: 700; }

.truncate {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.flex         { display: flex; }
.flex-col     { flex-direction: column; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.justify-center  { justify-content: center; }
.gap-xs  { gap: 0.25rem; }
.gap-sm  { gap: 0.5rem; }
.gap-md  { gap: 1rem; }
.gap-lg  { gap: 1.5rem; }
.gap-xl  { gap: 2rem; }

/* ===== Entity Detail Panel ===== */

/* Portrait Hero */
.char-detail-hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  padding: 1.5rem;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}

.char-detail-hero-portrait {
  width: 200px;
  height: 280px;
  object-fit: cover;
  object-position: top;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-subtle);
}

.char-detail-hero-portrait--creature {
  width: 200px;
  height: 200px;
}

.char-detail-hero-placeholder {
  width: 200px;
  height: 280px;
  border-radius: var(--radius-lg);
  border: 1px dashed var(--border-subtle);
  background: var(--bg-secondary);
}

.char-detail-hero-placeholder--creature {
  height: 200px;
}

.char-detail-hero-placeholder-spinner {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  gap: 0.75rem;
  color: var(--text-muted);
}

.char-detail-hero-placeholder-spinner-label {
  font-size: 0.8rem;
  font-family: var(--font-sans);
}

.char-detail-hero-name {
  font-family: var(--font-serif);
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--text-primary);
  margin: 0;
  text-align: center;
}

.char-detail-hero-epithet {
  font-family: var(--font-sans);
  font-size: 0.9rem;
  color: var(--text-secondary);
  margin: 0;
  text-align: center;
}

.char-detail-hero-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.25rem;
}

/* Section Headers */
.char-section-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 1.5rem 0 1rem;
  font-family: var(--font-sans);
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
}

.char-section-header::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border-subtle);
}

/* Prose Fields */
.char-prose-group {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  position: relative;
}

/* Name Generator */
.name-gen-panel {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
  margin-bottom: 1.5rem;
}

.name-gen-controls {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 1rem 1.5rem;
  align-items: end;
  margin-bottom: 1.5rem;
}

.name-gen-actions-row {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 1.5rem;
}

.name-gen-control-group {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  min-width: 0;
}

.name-gen-control-group select {
  font-family: var(--font-sans);
  font-size: 0.9rem;
  padding: 0.45em 0.8em;
  background: var(--bg-secondary);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius);
  color: var(--text-primary);
  min-width: 180px;
}

.name-gen-control-label {
  font-family: var(--font-sans);
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.name-gen-toggle-group {
  display: flex;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--border-subtle);
}

.name-gen-toggle {
  padding: 0.4em 0.8em;
  font-family: var(--font-sans);
  font-size: 0.85rem;
  background: var(--bg-secondary);
  color: var(--text-secondary);
  border: none;
  cursor: pointer;
  transition: background var(--transition), color var(--transition);
}

.name-gen-toggle:not(:last-child) {
  border-right: 1px solid var(--border-subtle);
}

.name-gen-toggle--active {
  background: var(--accent);
  color: var(--bg-primary);
  font-weight: 600;
}

.name-gen-results {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.name-gen-result {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5em 0.8em;
  border-radius: var(--radius);
  background: var(--bg-secondary);
  transition: background var(--transition);
}

.name-gen-result:hover {
  background: var(--bg-elevated);
}

.name-gen-result-name {
  font-family: var(--font-serif);
  font-size: 1.05rem;
  color: var(--text-primary);
}

.name-gen-result-actions {
  display: flex;
  gap: 0.25rem;
}

/* ==========================================================================
   Site-wide utilities
   ========================================================================== */

/* Site-wide icon tinting — inverts black icons to light for dark theme */
.civ-icon {
  filter: var(--icon-filter);
  vertical-align: middle;
  flex-shrink: 0;
}

/* Icons inside buttons — spacing and alignment */
.btn .civ-icon {
  margin-right: 0.35em;
  vertical-align: -0.1em;
}

/* On accent-bg buttons, icons match text color (dark on light) */
.btn-primary .civ-icon {
  filter: var(--icon-filter-on-accent);
}

/* Card elevation — consistent shadow treatment for any card */
.civ-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-surface);
  box-shadow: var(--shadow-sm);
  transition: box-shadow var(--transition), border-color var(--transition);
}

.civ-card:hover {
  box-shadow: var(--shadow-md);
}

/* Panel elevation — major container panels */
.civ-panel {
  border: 2px solid var(--border);
  border-radius: var(--radius-lg, 12px);
  background: var(--bg-surface);
  box-shadow: var(--shadow-md);
}

/* ── Print stylesheet ── */
@media print {
  nav, .toolbar, .sidebar, .action-bar, .footer-bar,
  .modal-overlay, .divider-picker, .tooltip,
  .editor-toolbar, .ai-toolbar, .margin-panel,
  .exploration-pane, .scene-cast-bar,
  button, .btn, [role="toolbar"] {
    display: none !important;
  }

  body {
    background: white !important;
    color: black !important;
    font-size: 12pt;
    line-height: 2;
    margin: 0;
    padding: 0;
  }

  .reading, .post-body, .ProseMirror {
    max-width: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
  }

  .section-divider-wrapper, .section-divider {
    display: flex !important;
    justify-content: center !important;
    margin: 2em 0 !important;
    page-break-inside: avoid;
  }

  .section-divider-wrapper svg, .section-divider svg {
    fill: black !important;
    stroke: black !important;
  }

  h1 { page-break-before: always; }
  h1:first-child { page-break-before: avoid; }
  p { orphans: 3; widows: 3; }
  img { max-width: 100% !important; page-break-inside: avoid; }
}

/* Version badge — admin/moderator only, fixed bottom-right */
.version-badge {
  position: fixed;
  bottom: 6px;
  right: 8px;
  font-family: var(--font-mono);
  font-size: 0.6rem;
  color: var(--text-muted);
  opacity: 0.35;
  cursor: pointer;
  z-index: 50;
  transition: opacity var(--transition);
  user-select: none;
}
.version-badge:hover { opacity: 0.7; }
