:root {
  --bg: #17171b;
  --bg-elev: #1f2025;
  --bg-elev-2: #26272d;
  --text: #ece9e6;
  --text-dim: #9a9aa2;
  --border: #34353c;
  --accent: #d98e4a;
  --accent-text: #1b1206;
  --danger: #e0645a;
  --radius: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

body.light {
  --bg: #f7f5f2;
  --bg-elev: #ffffff;
  --bg-elev-2: #eeece7;
  --text: #201f1d;
  --text-dim: #6b6963;
  --border: #ddd9d1;
  --accent: #a85c1b;
  --accent-text: #ffffff;
  color-scheme: light;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overscroll-behavior-y: none;
}

#app { height: 100%; position: relative; }

.screen {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  background: var(--bg);
}
.screen.active { display: flex; }

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(10px + var(--safe-top)) 10px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elev);
  flex: 0 0 auto;
}
.topbar-title { margin: 0; font-size: 17px; font-weight: 600; flex: 1; }
.brand { font-size: 19px; margin: 0; flex: 1; font-weight: 700; letter-spacing: 0.2px; }
.topbar-actions { display: flex; gap: 4px; }

.icon-btn {
  background: none;
  border: none;
  color: var(--text);
  padding: 8px;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:active { background: var(--bg-elev-2); }

/* ---------- Projects screen ---------- */
.project-list {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 10px 12px 90px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.project-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.project-card:active { background: var(--bg-elev-2); }
.project-card-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.project-title { font-size: 16px; font-weight: 600; }
.project-type {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 999px;
  padding: 2px 8px;
  flex: 0 0 auto;
}
.project-meta { font-size: 13px; color: var(--text-dim); }

.empty-state { color: var(--text-dim); text-align: center; padding: 60px 30px; font-size: 15px; }

.fab {
  position: absolute;
  right: 20px;
  bottom: calc(24px + var(--safe-bottom));
  width: 58px;
  height: 58px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 30px;
  line-height: 1;
  box-shadow: 0 4px 14px rgba(0,0,0,0.35);
}
.fab:active { transform: scale(0.96); }

/* ---------- Editor screen ---------- */
.editor-topbar { gap: 6px; }
.editor-title {
  flex: 1;
  background: none;
  border: none;
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  padding: 6px 4px;
  min-width: 0;
}
.editor-title:focus { outline: none; border-bottom: 1px solid var(--accent); }
.save-indicator { font-size: 11px; color: var(--text-dim); flex: 0 0 auto; min-width: 44px; text-align: right; }

.editor-scroll {
  flex: 1 1 auto;
  overflow-y: auto;
  background: var(--bg);
  -webkit-overflow-scrolling: touch;
}

.editor {
  max-width: 720px;
  margin: 0 auto;
  padding: 24px 18px 260px;
  font-family: "Courier New", Courier, monospace;
  font-size: 15.5px;
  line-height: 1.5;
  color: var(--text);
}

.block {
  outline: none;
  white-space: pre-wrap;
  word-wrap: break-word;
  padding: 2px 0;
  min-height: 1.5em;
  border-left: 3px solid transparent;
}
.block:empty::before {
  content: attr(data-placeholder);
  color: var(--text-dim);
  opacity: 0.7;
}
.block.focused { border-left-color: var(--accent); background: rgba(217,142,74,0.06); }

/* Screenplay element styling */
.block[data-type="scene-heading"] { text-transform: uppercase; font-weight: 700; margin-top: 1.2em; }
.block[data-type="action"] { }
.block[data-type="character"] { text-transform: uppercase; margin-left: 35%; margin-top: 1em; font-weight: 600; }
.block[data-type="parenthetical"] { margin-left: 28%; margin-right: 20%; color: var(--text-dim); }
.block[data-type="dialogue"] { margin-left: 18%; margin-right: 18%; }
.block[data-type="transition"] { text-transform: uppercase; text-align: right; margin-top: 1em; font-weight: 600; }
.block[data-type="shot"] { text-transform: uppercase; font-weight: 600; margin-top: 1em; }

/* Stage play element styling */
.block[data-type="act-scene"] { text-transform: uppercase; font-weight: 700; text-align: center; margin-top: 1.4em; }
.block[data-type="stage-direction"] { font-style: italic; color: var(--text-dim); margin: 0.4em 8%; }
.block[data-type="stage-character"] { text-transform: uppercase; text-align: center; margin-top: 1em; font-weight: 700; letter-spacing: 0.5px; }
.block[data-type="stage-dialogue"] { margin: 0 6%; }
.block[data-type="stage-parenthetical"] { margin-left: 30%; margin-right: 20%; color: var(--text-dim); font-style: italic; }

/* Element toolbar (mobile) */
.element-toolbar {
  flex: 0 0 auto;
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 8px calc(10px + var(--safe-bottom)) calc(10px + var(--safe-bottom)) 10px;
  background: var(--bg-elev);
  border-top: 1px solid var(--border);
  -webkit-overflow-scrolling: touch;
}
.el-btn {
  flex: 0 0 auto;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 999px;
  padding: 9px 14px;
  font-size: 13px;
  white-space: nowrap;
}
.el-btn.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

/* ---------- Settings ---------- */
.settings-body { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 22px; }
.settings-group h3 { margin: 0 0 10px; font-size: 14px; text-transform: uppercase; letter-spacing: 0.4px; color: var(--text-dim); display: flex; align-items: center; gap: 8px; }
.badge { font-size: 10px; text-transform: none; letter-spacing: 0; background: var(--bg-elev-2); border: 1px solid var(--border); border-radius: 999px; padding: 1px 8px; color: var(--text-dim); }
.hint { font-size: 13px; color: var(--text-dim); line-height: 1.5; margin: 6px 0; }
.row { display: flex; align-items: center; justify-content: space-between; background: var(--bg-elev); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; font-size: 15px; }
.row input[type=checkbox] { width: 20px; height: 20px; }
.row-buttons { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.drive-status { font-weight: 600; }

.field { display: flex; flex-direction: column; gap: 6px; margin-top: 10px; font-size: 13px; color: var(--text-dim); }
.field input, .field select {
  font-size: 15px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text);
}

.btn {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
}
.btn:active { transform: scale(0.98); }
.btn-primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn-danger { background: var(--danger); color: #2a0d0a; border-color: var(--danger); }

/* ---------- Modals ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.55); z-index: 50;
}
.modal {
  position: fixed;
  left: 50%; bottom: 0;
  transform: translateX(-50%);
  width: min(480px, 100vw);
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: 16px 16px 0 0;
  padding: 18px 18px calc(18px + var(--safe-bottom));
  z-index: 51;
  max-height: 80vh;
  overflow-y: auto;
}
@media (min-width: 560px) {
  .modal { bottom: auto; top: 50%; transform: translate(-50%, -50%); border-radius: 16px; border-bottom: 1px solid var(--border); }
}
.modal h3 { margin: 0 0 8px; font-size: 17px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 16px; }
.menu-list { display: flex; flex-direction: column; gap: 2px; margin-top: 10px; }
.menu-item {
  background: none; border: none; color: var(--text); text-align: left;
  padding: 13px 8px; font-size: 15px; border-radius: 8px;
}
.menu-item:active { background: var(--bg-elev-2); }
.menu-item.danger { color: var(--danger); }
.menu-sep { height: 1px; background: var(--border); margin: 6px 0; }

.toast {
  position: fixed;
  left: 50%; bottom: calc(20px + var(--safe-bottom));
  transform: translateX(-50%);
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 60;
  max-width: 90vw;
  text-align: center;
}

.print-root { display: none; }

/* ---------- Print (PDF export) ---------- */
@media print {
  #app { position: static !important; height: auto !important; }
  .screen, .modal, .modal-backdrop, .toast {
    display: none !important;
  }
  body, html { background: #fff; }
  .print-root {
    display: block;
    font-family: "Courier New", Courier, monospace;
    font-size: 12pt;
    line-height: 1;
    color: #000;
    width: 6in;
    margin: 0 auto;
  }
  @page { size: 8.5in 11in; margin: 1in 1in 1in 1.5in; }
  .print-root .p-scene-heading { text-transform: uppercase; font-weight: bold; margin: 0 0 1em; }
  .print-root .p-action { margin: 0 0 1em; }
  .print-root .p-character { text-transform: uppercase; margin: 0 0 0 2.2in; }
  .print-root .p-parenthetical { margin: 0 0 0 1.6in; width: 2in; }
  .print-root .p-dialogue { margin: 0 1in 1em 1in; }
  .print-root .p-transition { text-transform: uppercase; text-align: right; margin: 0 0 1em; }
  .print-root .p-shot { text-transform: uppercase; font-weight: bold; margin: 0 0 1em; }
  .print-root .p-act-scene { text-transform: uppercase; font-weight: bold; text-align: center; margin: 0 0 1.4em; }
  .print-root .p-stage-direction { font-style: italic; margin: 0 0 1em 0.5in; width: 5in; }
  .print-root .p-stage-character { text-transform: uppercase; text-align: center; font-weight: bold; margin: 1em 0 0; }
  .print-root .p-stage-dialogue { text-align: center; margin: 0 0 1em; }
  .print-root .p-stage-parenthetical { text-align: center; font-style: italic; margin: 0 0 0.3em; }
  .print-root .p-title-page { text-align: center; margin-top: 3in; page-break-after: always; }
  .print-root .p-title-page h1 { text-transform: uppercase; font-size: 14pt; }
}
