/* Gothic night palette */
:root {
  --bg-black: #07060b;
  --bg-panel: #1a1030;
  --accent-amber: #ffb347;
  --text-bone: #efe6d2;
  --graveyard-sky: #0d0620;
  --graveyard-ground: #1a1030;
  --moon-purified: #e8ecf1;
  --moon-cursed: #b3001b;
  --focus-ring: #ffb347;
  --green-glow: #2ecc71;
  --red-strike: #e74c3c;
}

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

body {
  font-family: Georgia, 'Times New Roman', serif;
  background: var(--bg-black);
  color: var(--text-bone);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Header */
header {
  background: var(--bg-panel);
  padding: 1.5rem 2rem;
  border-bottom: 2px solid var(--accent-amber);
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 1.5rem;
}

.header-content {
  flex: 1;
  min-width: 300px;
}

h1 {
  font-size: 2.5rem;
  color: var(--accent-amber);
  animation: candle-flicker 3s ease-in-out infinite;
  margin-bottom: 0.25rem;
}

@keyframes candle-flicker {
  0%, 100% { opacity: 1; text-shadow: 0 0 20px var(--accent-amber); }
  50% { opacity: 0.95; text-shadow: 0 0 15px var(--accent-amber); }
}

.tagline {
  font-size: 1rem;
  opacity: 0.8;
  margin-bottom: 1rem;
}

.controls {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

button {
  background: var(--bg-black);
  color: var(--text-bone);
  border: 1px solid var(--accent-amber);
  padding: 0.5rem 1rem;
  font-family: inherit;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.2s;
}

button:hover:not(:disabled) {
  background: var(--accent-amber);
  color: var(--bg-black);
}

button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

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

/* Haunt Meter */
.haunt-meter {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.moon {
  color: var(--moon-purified);
  transition: color 0.5s;
}

body[data-band="Restless"] .moon { color: #d4b896; }
body[data-band="Haunted"] .moon { color: #d4896e; }
body[data-band="Cursed"] .moon { color: var(--moon-cursed); }

.meter-text {
  text-align: left;
}

.score {
  font-size: 2.5rem;
  font-weight: bold;
  line-height: 1;
  font-family: system-ui, sans-serif;
}

.band {
  font-size: 1.2rem;
  font-weight: bold;
  margin-top: 0.25rem;
}

.blurb {
  font-size: 0.85rem;
  opacity: 0.7;
  margin-top: 0.25rem;
}

/* Main layout */
main {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2rem;
  padding: 2rem;
  max-width: 1800px;
  margin: 0 auto;
  width: 100%;
}

section h2 {
  font-size: 1.5rem;
  color: var(--accent-amber);
  margin-bottom: 1rem;
}

/* Grimoire Editor */
.grimoire-section {
  display: flex;
  flex-direction: column;
}

.editor-container {
  flex: 1;
  display: flex;
  background: var(--bg-panel);
  border: 2px solid var(--accent-amber);
  border-radius: 4px;
  overflow: hidden;
  position: relative;
  min-height: 500px;
}

.line-numbers {
  padding: 1rem 0.5rem;
  background: rgba(0, 0, 0, 0.3);
  color: rgba(239, 230, 210, 0.4);
  font-family: 'Courier New', monospace;
  font-size: 0.9rem;
  line-height: 1.5;
  text-align: right;
  user-select: none;
  overflow: hidden;
  min-width: 3rem;
}

.editor-wrapper {
  flex: 1;
  position: relative;
  overflow: auto;
}

.highlight-overlay,
.grimoire-editor {
  font-family: 'Courier New', monospace;
  font-size: 0.9rem;
  line-height: 1.5;
  padding: 1rem;
  margin: 0;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.highlight-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  color: transparent;
  pointer-events: none;
  overflow: hidden;
}

.grimoire-editor {
  position: relative;
  width: 100%;
  height: 100%;
  background: transparent;
  color: var(--text-bone);
  border: none;
  outline: none;
  resize: none;
  z-index: 1;
}

.highlight-overlay mark {
  background: transparent;
  color: transparent;
  border-bottom: 2px solid currentColor;
  border-radius: 2px;
}

/* Graveyard */
.graveyard-section {
  display: flex;
  flex-direction: column;
}

.graveyard {
  flex: 1;
  position: relative;
  background: linear-gradient(to bottom, var(--graveyard-sky) 0%, #1a0f30 60%, var(--graveyard-ground) 100%);
  border: 2px solid var(--accent-amber);
  border-radius: 4px;
  overflow: auto;
  min-height: 500px;
  padding: 2rem;
}

.sky {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 60%;
  pointer-events: none;
}

.stars {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 60%;
  background-image: 
    radial-gradient(circle at 20% 20%, white 1px, transparent 1px),
    radial-gradient(circle at 60% 10%, white 1px, transparent 1px),
    radial-gradient(circle at 80% 30%, white 1px, transparent 1px),
    radial-gradient(circle at 40% 40%, white 1px, transparent 1px),
    radial-gradient(circle at 90% 50%, white 1px, transparent 1px);
  background-size: 100% 100%;
  opacity: 0.6;
  pointer-events: none;
}

.fog {
  position: absolute;
  left: -10%;
  width: 120%;
  height: 150px;
  background: radial-gradient(ellipse, rgba(200, 200, 220, 0.15), transparent);
  pointer-events: none;
  opacity: 0.5;
  animation: fog-drift 20s ease-in-out infinite;
}

.fog-1 {
  bottom: 25%;
  animation-delay: 0s;
}

.fog-2 {
  bottom: 15%;
  animation-delay: 10s;
}

@keyframes fog-drift {
  0%, 100% { transform: translateX(0); }
  50% { transform: translateX(5%); }
}

body[data-band="Purified"] .fog {
  opacity: 0;
  background: radial-gradient(ellipse, rgba(255, 230, 150, 0.1), transparent);
}

.fence {
  position: absolute;
  bottom: 20%;
  left: 0;
  right: 0;
  height: 80px;
  background: repeating-linear-gradient(
    90deg,
    transparent,
    transparent 20px,
    rgba(0, 0, 0, 0.6) 20px,
    rgba(0, 0, 0, 0.6) 25px
  );
  pointer-events: none;
}

.ground {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 20%;
  background: var(--graveyard-ground);
  pointer-events: none;
}

.tombstones {
  position: relative;
  z-index: 10;
  display: flex;
  flex-wrap: wrap;
  gap: 1.5rem;
  padding-bottom: 2rem;
}

.tombstone {
  position: relative;
  width: 140px;
  min-height: 100px;
  background: linear-gradient(135deg, #666 0%, #444 100%);
  border-radius: 50% 50% 0 0;
  padding: 1rem 0.5rem;
  text-align: center;
  font-size: 0.75rem;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.6);
  transition: box-shadow 0.3s;
}

.tombstone.at-rest {
  box-shadow: 0 4px 8px rgba(46, 204, 113, 0.4), 0 0 20px rgba(46, 204, 113, 0.2);
}

.tombstone-label {
  font-weight: bold;
  margin-bottom: 0.5rem;
  font-family: system-ui, sans-serif;
}

.tombstone-text {
  font-size: 0.7rem;
  opacity: 0.7;
  word-break: break-word;
}

.ghosts {
  position: absolute;
  top: -60px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
}

.ghost {
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  animation: ghost-bob 2s ease-in-out infinite;
  transition: transform 0.2s;
}

.ghost:nth-child(2) { animation-delay: 0.3s; }
.ghost:nth-child(3) { animation-delay: 0.6s; }

.ghost:hover,
.ghost:focus {
  transform: scale(1.1);
}

@keyframes ghost-bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-8px); }
}

.ghost-badge {
  display: inline-block;
  background: var(--accent-amber);
  color: var(--bg-black);
  font-size: 0.7rem;
  padding: 0.25rem 0.4rem;
  border-radius: 50%;
  font-weight: bold;
}

/* Bestiary */
.bestiary {
  position: fixed;
  top: 0;
  right: 0;
  width: 350px;
  height: 100vh;
  background: var(--bg-panel);
  border-left: 2px solid var(--accent-amber);
  z-index: 200;
  padding: 2rem;
  overflow-y: auto;
  transform: translateX(100%);
  transition: transform 0.3s;
}

.bestiary:not([hidden]) {
  transform: translateX(0);
}

.bestiary-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
}

.bestiary-header h2 {
  margin: 0;
}

#close-bestiary {
  background: none;
  border: none;
  font-size: 1.5rem;
  padding: 0.25rem;
}

.ghost-entry {
  margin-bottom: 1.5rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid rgba(239, 230, 210, 0.2);
}

.ghost-entry:last-child {
  border-bottom: none;
}

.ghost-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 0.5rem;
}

.ghost-name {
  font-size: 1.1rem;
  font-weight: bold;
}

.ghost-count {
  margin-left: auto;
  background: var(--accent-amber);
  color: var(--bg-black);
  padding: 0.25rem 0.5rem;
  border-radius: 12px;
  font-size: 0.85rem;
  font-weight: bold;
}

.ghost-summary {
  font-size: 0.9rem;
  opacity: 0.8;
  margin-top: 0.5rem;
}

/* Exorcism Card */
.exorcism-card {
  background: var(--bg-panel);
  color: var(--text-bone);
  border: 3px solid var(--accent-amber);
  border-radius: 8px;
  padding: 2rem;
  max-width: 600px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.8);
}

.exorcism-card::backdrop {
  background: rgba(0, 0, 0, 0.8);
}

.card-ghost-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.card-ghost-name {
  font-size: 1.5rem;
  font-weight: bold;
}

.card-line {
  background: rgba(0, 0, 0, 0.3);
  padding: 1rem;
  border-radius: 4px;
  font-family: 'Courier New', monospace;
  margin: 1rem 0;
  position: relative;
}

.card-line mark {
  background: rgba(255, 179, 71, 0.3);
  padding: 0.1rem 0.2rem;
  border-radius: 2px;
}

.card-why {
  margin: 1rem 0;
  padding: 1rem;
  background: rgba(255, 179, 71, 0.1);
  border-left: 3px solid var(--accent-amber);
  border-radius: 4px;
}

.card-diff {
  margin: 1rem 0;
}

.card-diff h4 {
  margin-bottom: 0.5rem;
  color: var(--accent-amber);
}

.diff-old {
  color: var(--red-strike);
  text-decoration: line-through;
  font-family: 'Courier New', monospace;
  margin: 0.5rem 0;
}

.diff-new {
  color: var(--green-glow);
  font-family: 'Courier New', monospace;
  margin: 0.5rem 0;
}

.card-actions {
  display: flex;
  gap: 1rem;
  margin-top: 1.5rem;
}

.card-actions button {
  flex: 1;
}

/* Export Dialog */
.export-dialog {
  background: var(--bg-panel);
  color: var(--text-bone);
  border: 3px solid var(--accent-amber);
  border-radius: 8px;
  padding: 2rem;
  max-width: 700px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}

.export-dialog::backdrop {
  background: rgba(0, 0, 0, 0.8);
}

.export-dialog h2 {
  margin-bottom: 1rem;
}

.export-preview {
  flex: 1;
  background: rgba(0, 0, 0, 0.3);
  padding: 1rem;
  border-radius: 4px;
  overflow: auto;
  font-family: 'Courier New', monospace;
  font-size: 0.85rem;
  margin-bottom: 1rem;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.export-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 2rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--green-glow);
  color: white;
  padding: 1rem 2rem;
  border-radius: 8px;
  font-size: 1.2rem;
  font-weight: bold;
  box-shadow: 0 4px 20px rgba(46, 204, 113, 0.5);
  z-index: 300;
  animation: toast-in 0.3s;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateX(-50%) translateY(20px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* Sample Menu */
.sample-menu {
  position: absolute;
  top: 100%;
  left: 0;
  margin-top: 0.25rem;
  background: var(--bg-panel);
  color: var(--text-bone);
  border: 2px solid var(--accent-amber);
  font-family: inherit;
  font-size: 0.9rem;
  min-width: 200px;
  z-index: 150;
}

.sample-menu option {
  padding: 0.5rem;
}

/* Footer */
footer {
  background: var(--bg-panel);
  border-top: 2px solid var(--accent-amber);
  padding: 1.5rem 2rem;
  text-align: center;
  font-size: 0.9rem;
  opacity: 0.8;
}

footer p {
  margin: 0.25rem 0;
}

footer a {
  color: var(--accent-amber);
  text-decoration: none;
}

footer a:hover {
  text-decoration: underline;
}

/* Reduced Motion */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  
  .fog {
    display: none;
  }
  
  .ghost {
    animation: none !important;
  }
}

/* Responsive */
@media (max-width: 900px) {
  main {
    grid-template-columns: 1fr;
  }
  
  .header-content {
    min-width: 100%;
  }
  
  header {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .bestiary {
    width: 100%;
  }
}

/* ---- Polish pass (alignment, legibility, ghost colours) ---- */
[hidden] { display: none !important; }

.line-numbers {
  white-space: pre;
  padding: 1rem 0.6rem 1rem 0.4rem;
  font-family: ui-monospace, 'SFMono-Regular', Menlo, Consolas, 'Courier New', monospace;
  font-size: 0.9rem;
  line-height: 1.5;
}
.line-numbers .haunted { color: #ff8fa3; font-weight: 700; }

.editor-wrapper { overflow: hidden; }
.highlight-overlay,
.grimoire-editor {
  font-family: ui-monospace, 'SFMono-Regular', Menlo, Consolas, 'Courier New', monospace;
  font-size: 0.9rem;
  line-height: 1.5;
  white-space: pre;
  overflow-wrap: normal;
  word-wrap: normal;
  tab-size: 2;
  letter-spacing: 0;
}
.highlight-overlay { bottom: 0; overflow: hidden; color: transparent; }
.grimoire-editor { overflow: auto; caret-color: var(--accent-amber); min-height: 500px; }
.highlight-overlay mark {
  color: transparent;
  background: color-mix(in srgb, var(--ghost, #b48cff) 28%, transparent);
  border-bottom: 2px solid var(--ghost, #b48cff);
  border-radius: 3px;
  box-shadow: 0 0 10px color-mix(in srgb, var(--ghost, #b48cff) 45%, transparent);
}
.highlight-overlay .wandering-line {
  background: linear-gradient(90deg, rgba(62, 230, 176, 0.12), transparent 80%);
  border-left: 2px solid #3ee6b0;
  margin-left: -2px;
}
.editor-hint { margin-top: 0.5rem; font-size: 0.85rem; color: rgba(239, 230, 210, 0.75); }

.graveyard { min-height: 520px; }
.tombstones {
  column-gap: 1.25rem;
  row-gap: 4.5rem;
  padding: 4rem 1rem 2.5rem;
  justify-content: center;
}
.tombstone {
  width: 150px;
  min-height: 112px;
  padding: 1.1rem 0.7rem 0.8rem;
  background: linear-gradient(160deg, #6d6a75 0%, #47444f 55%, #34313b 100%);
  border: 1px solid rgba(0, 0, 0, 0.4);
  color: #f3eee2;
}
.tombstone::after {
  content: '';
  position: absolute; left: -10px; right: -10px; bottom: -6px; height: 8px;
  background: #1d2a1d; border-radius: 50%; filter: blur(1px);
}
.tombstone-label { font-family: Georgia, 'Times New Roman', serif; letter-spacing: 0.08em; color: #fff; }
.tombstone-text { opacity: 0.92; font-size: 0.72rem; line-height: 1.3; color: #ece6d8; }
.tombstone.at-rest { box-shadow: 0 0 0 1px rgba(62, 230, 176, 0.35), 0 0 22px rgba(62, 230, 176, 0.25); }
.tombstone.at-rest .tombstone-label::after { content: ' · at rest'; color: #7ff0c8; font-size: 0.7rem; letter-spacing: 0; }
.ghosts { top: -58px; gap: 0.1rem; pointer-events: none; }
.ghost { pointer-events: auto; border-radius: 50%; line-height: 0; }
.ghost svg { overflow: visible; }
.ghost:focus-visible { outline: 3px solid var(--accent-amber); outline-offset: 2px; }
.ghost-badge { align-self: center; border-radius: 999px; }

body[data-band="Purified"] .graveyard::after {
  content: '';
  position: absolute; inset: 0; pointer-events: none;
  background: radial-gradient(ellipse at 50% 110%, rgba(255, 214, 140, 0.25), transparent 60%);
}
.graveyard { position: relative; }
.graveyard-empty { color: #efe6d2; text-align: center; padding: 3rem 1rem; position: relative; z-index: 11; }

.sample-menu { position: fixed; }
#toggle-sound { min-width: 7.5rem; }

@media (prefers-reduced-motion: reduce) {
  .ghost, .fog, h1 { animation: none !important; }
}
dialog:not([open]) { display: none !important; }
dialog.exorcism-card, dialog.export-dialog { margin: auto; position: fixed; inset: 0; max-height: 90vh; }
.ghosts { top: -46px; }
.sample-label { font-size: 0.85rem; color: #efe6d2; align-self: center; }
.sample-select {
  background: var(--bg-panel, #1a1030); color: #efe6d2; border: 1px solid var(--accent-amber, #ffb347);
  padding: 0.45rem 0.6rem; font: inherit; font-size: 0.95rem; border-radius: 2px;
}
.sample-select:focus-visible { outline: 3px solid var(--accent-amber, #ffb347); outline-offset: 2px; }
.editor-hint { margin: -0.4rem 0 0.8rem; }
.graveyard, .editor-container { max-height: 80vh; }
