/* ═══════════════════════════════════════════════════════════════════════════
   SCREENPLAY UI REFRESH
   Drop-in layer for screenplay.html — reduces chrome, adds focus mode,
   command palette, overflow menu, and fixes the mobile experience.

   Load this LAST, after all other screenplay stylesheets:
     <link rel="stylesheet" href="screenplay-ui-refresh.css">
   ═══════════════════════════════════════════════════════════════════════════ */

/* ─────────────────────────────────────────────────────────────────────────
   0. TOKENS — everything below uses these instead of hardcoded rgba()
   ───────────────────────────────────────────────────────────────────────── */
:root {
  --sp-accent: var(--color-primary, #5a189a);
  --sp-accent-soft: color-mix(in srgb, var(--sp-accent) 10%, transparent);
  --sp-accent-line: color-mix(in srgb, var(--sp-accent) 25%, transparent);
  --sp-surface: var(--bg-glass-panel, rgba(255, 255, 255, 0.9));
  --sp-surface-solid: #fff;
  --sp-line: var(--border-glass, rgba(0, 0, 0, 0.1));
  --sp-text: var(--color-text, #1a1a2e);
  --sp-text-dim: var(--color-soft-gray, #6b7280);
  --sp-radius: 12px;
  --sp-shadow-pop: 0 12px 40px rgba(0, 0, 0, 0.16);
  --sp-tap: 44px; /* minimum touch target */
}

body.dark {
  --sp-surface-solid: #16112a;
  --sp-line: rgba(255, 255, 255, 0.12);
  --sp-shadow-pop: 0 12px 40px rgba(0, 0, 0, 0.6);
}

/* ─────────────────────────────────────────────────────────────────────────
   1. OVERFLOW ("More") MENU — holds the buttons removed from the header
   ───────────────────────────────────────────────────────────────────────── */
.sp-more-wrapper {
  position: relative;
  display: inline-flex;
}

.sp-more-menu {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 1200;
  min-width: 220px;
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  background: var(--sp-surface-solid);
  border: 1px solid var(--sp-line);
  border-radius: var(--sp-radius);
  box-shadow: var(--sp-shadow-pop);
}

.sp-more-menu[hidden] {
  display: none;
}

/* Buttons relocated into the menu render as full-width rows */
.sp-more-menu .action-btn,
.sp-more-menu .read-aloud-wrapper {
  width: 100%;
  justify-content: flex-start !important;
  border: none !important;
  background: transparent !important;
  box-shadow: none !important;
  border-radius: 8px !important;
  padding: 10px 12px !important;
  min-height: var(--sp-tap);
  font-size: 0.85rem;
}

.sp-more-menu .action-btn:hover {
  background: var(--sp-accent-soft) !important;
}

.sp-more-menu .action-btn span {
  display: inline !important;
}

.sp-more-menu .read-aloud-wrapper {
  display: block;
  padding: 0 !important;
}

.sp-more-menu .read-speed-popover {
  position: static;
  margin: 4px 0 0;
  width: 100%;
}

/* ─────────────────────────────────────────────────────────────────────────
   2. FOCUS MODE — hides all chrome, reveals on hover near the top
   ───────────────────────────────────────────────────────────────────────── */
body.sp-focus .header-section,
body.sp-focus .meta-bar,
body.sp-focus .screenplay-toolbar {
  transform: translateY(-110%);
  opacity: 0;
  pointer-events: none;
  transition: transform 0.25s ease, opacity 0.25s ease;
}

body.sp-focus .header-section {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
}

body.sp-focus .sp-reveal-zone {
  display: block;
}

body.sp-focus.sp-reveal .header-section {
  transform: translateY(0);
  opacity: 1;
  pointer-events: auto;
}

body.sp-focus .editor-scroll-area {
  padding-top: 24px;
}

.sp-reveal-zone {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 14px;
  z-index: 1150;
}

.sp-focus-exit {
  display: none;
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 1150;
  gap: 8px;
  align-items: center;
  padding: 10px 16px;
  min-height: var(--sp-tap);
  border: 1px solid var(--sp-line);
  border-radius: 999px;
  background: var(--sp-surface-solid);
  color: var(--sp-text);
  box-shadow: var(--sp-shadow-pop);
  cursor: pointer;
  font-size: 0.8rem;
}

body.sp-focus .sp-focus-exit {
  display: inline-flex;
}

/* ─────────────────────────────────────────────────────────────────────────
   3. COMMAND PALETTE (Ctrl/Cmd+K)
   ───────────────────────────────────────────────────────────────────────── */
.sp-palette-backdrop {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 12vh 16px 16px;
  background: rgba(10, 8, 20, 0.45);
  backdrop-filter: blur(4px);
}

.sp-palette-backdrop[hidden] {
  display: none;
}

.sp-palette {
  width: min(560px, 100%);
  max-height: 60vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--sp-surface-solid);
  border: 1px solid var(--sp-line);
  border-radius: 16px;
  box-shadow: var(--sp-shadow-pop);
}

.sp-palette-input {
  width: 100%;
  padding: 16px 18px;
  border: none;
  border-bottom: 1px solid var(--sp-line);
  background: transparent;
  color: var(--sp-text);
  font-size: 1rem;
  outline: none;
}

.sp-palette-list {
  overflow-y: auto;
  padding: 6px;
}

.sp-palette-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 11px 12px;
  min-height: var(--sp-tap);
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--sp-text);
  font-size: 0.9rem;
  text-align: left;
  cursor: pointer;
}

.sp-palette-item i {
  width: 18px;
  color: var(--sp-text-dim);
}

.sp-palette-item--active,
.sp-palette-item:hover {
  background: var(--sp-accent-soft);
}

.sp-palette-item--active i,
.sp-palette-item:hover i {
  color: var(--sp-accent);
}

.sp-palette-key {
  margin-left: auto;
  padding: 2px 7px;
  border: 1px solid var(--sp-line);
  border-radius: 5px;
  color: var(--sp-text-dim);
  font-size: 0.7rem;
  font-family: ui-monospace, monospace;
}

.sp-palette-empty {
  padding: 24px;
  text-align: center;
  color: var(--sp-text-dim);
  font-size: 0.85rem;
}

/* ─────────────────────────────────────────────────────────────────────────
   4. META BAR — 3 stats by default, rest behind a toggle
   ───────────────────────────────────────────────────────────────────────── */
.meta-bar .meta-input.sp-stat-extra {
  display: none;
}

body.sp-stats-open .meta-bar .meta-input.sp-stat-extra {
  display: flex;
}

.sp-stats-toggle {
  margin-left: auto;
  padding: 4px 10px;
  min-height: 32px;
  border: 1px solid var(--sp-line);
  border-radius: 999px;
  background: transparent;
  color: var(--sp-text-dim);
  font-size: 0.72rem;
  cursor: pointer;
}

.sp-stats-toggle:hover {
  border-color: var(--sp-accent);
  color: var(--sp-accent);
}

/* ─────────────────────────────────────────────────────────────────────────
   5. ELEMENT TYPE ROW — collapses to a single pill on narrow screens
   ───────────────────────────────────────────────────────────────────────── */
.sp-element-pill {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  min-height: var(--sp-tap);
  border: 1px solid var(--sp-accent-line);
  border-radius: 999px;
  background: var(--sp-accent-soft);
  color: var(--sp-accent);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
}

/* Hide the long hint string — it lives in the palette / shortcuts modal now */
.screenplay-toolbar .toolbar-label[style*="margin-left"] {
  display: none;
}

@media (max-width: 900px) {
  .sp-element-pill {
    display: inline-flex;
  }

  .screenplay-toolbar .toolbar-group:first-child .element-type-btn {
    display: none;
  }

  body.sp-elements-open .screenplay-toolbar .toolbar-group:first-child .element-type-btn {
    display: inline-flex;
  }

  .screenplay-toolbar .toolbar-label {
    display: none;
  }

  .element-type-shortcut {
    display: none;
  }
}

/* ─────────────────────────────────────────────────────────────────────────
   6. MOBILE — AI panel as a bottom sheet, scene nav as an overlay drawer
   ───────────────────────────────────────────────────────────────────────── */
.sp-drawer-backdrop {
  position: fixed;
  inset: 0;
  z-index: 899;
  background: rgba(10, 8, 20, 0.4);
}

.sp-drawer-backdrop[hidden] {
  display: none;
}

@media (max-width: 768px) {
  /* AI panel → bottom sheet */
  .sp-ai-panel {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    max-width: 100%;
    height: 82vh;
    border-left: none;
    border-top: 1px solid var(--sp-line);
    border-radius: 18px 18px 0 0;
    transform: translateY(100%);
  }

  .sp-ai-panel--open {
    transform: translateY(0);
  }

  /* Grab handle */
  .sp-ai-panel::before {
    content: "";
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 4px;
    border-radius: 999px;
    background: var(--sp-line);
  }

  .sp-ai-panel-header {
    padding-top: 20px;
  }

  /* The floating edge tab is replaced by the header AI button */
  .sp-ai-toggle-btn {
    display: none !important;
  }

  /* Scene navigator → overlay drawer */
  .scene-nav {
    position: fixed !important;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 900;
    width: min(280px, 84vw);
    background: var(--sp-surface-solid);
    box-shadow: var(--sp-shadow-pop);
  }

  /* Touch targets */
  .back-btn,
  .scene-nav-close,
  .frp-close,
  .sp-ai-close-btn {
    width: var(--sp-tap);
    height: var(--sp-tap);
  }

  .header-actions .action-btn {
    min-height: var(--sp-tap);
  }

  /* Header: labels off, icons only — the overflow menu keeps names */
  .header-actions > .action-btn span:not(.sp-keep-label) {
    display: none;
  }

  .meta-bar {
    font-size: 0.72rem;
    overflow-x: auto;
    white-space: nowrap;
  }

  .find-replace-panel {
    left: 8px;
    right: 8px;
    width: auto;
  }
}

/* ─────────────────────────────────────────────────────────────────────────
   7. AI ENTRY POINT — one labelled header button instead of a floating tab
   ───────────────────────────────────────────────────────────────────────── */
.sp-ai-header-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  min-height: var(--sp-tap);
  border: 1px solid var(--sp-accent-line);
  border-radius: 999px;
  background: var(--sp-accent-soft);
  color: var(--sp-accent);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.sp-ai-header-btn:hover {
  background: var(--sp-accent);
  color: #fff;
}

/* Once a header button exists, demote the floating edge tab everywhere */
body.sp-ai-header-btn-present .sp-ai-toggle-btn {
  display: none !important;
}

/* Lines touched by AI get a gutter accent so the edit stays traceable */
.sp-line.sp-ai-touched {
  position: relative;
}

.sp-line.sp-ai-touched::before {
  content: "";
  position: absolute;
  left: -10px;
  top: 2px;
  bottom: 2px;
  width: 3px;
  border-radius: 2px;
  background: var(--sp-accent);
  opacity: 0.6;
}

/* ─────────────────────────────────────────────────────────────────────────
   8. SHORTCUTS MODAL
   ───────────────────────────────────────────────────────────────────────── */
.sp-shortcuts-grid {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px 24px;
  padding: 4px 2px;
  font-size: 0.86rem;
}

.sp-shortcuts-grid kbd {
  padding: 2px 7px;
  border: 1px solid var(--sp-line);
  border-radius: 5px;
  background: transparent;
  font-family: ui-monospace, monospace;
  font-size: 0.75rem;
}

@media (prefers-reduced-motion: reduce) {
  .sp-ai-panel,
  body.sp-focus .header-section,
  body.sp-focus .meta-bar,
  body.sp-focus .screenplay-toolbar {
    transition: none !important;
  }
}
