/* Match the character generator's editorial theme. */
:root {
  color-scheme: light;
  --bg-color: #fafafa;
  --panel-bg: #fff;
  --panel-border: #e5e5e5;
  --field-border: #d1d5db;
  --text-main: #111827;
  --text-muted: #6b7280;
  --accent-secondary: #374151;
  --font-ui: Inter, system-ui, -apple-system, sans-serif;
  --font-heading: "Playfair Display", Georgia, serif;
  --font-script: "Courier New", Courier, monospace;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg-color);
  color: var(--text-main);
  font: 1rem/1.6 var(--font-ui);
}

h1, h2 {
  margin: 0;
  font-family: var(--font-heading);
}

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

button {
  min-height: 44px;
  padding: 0.5rem 0.65rem;
  border: 1px solid transparent;
  border-radius: 4px;
  color: var(--text-muted);
  background: transparent;
  font-size: 0.85rem;
  cursor: pointer;
}

button:hover:not(:disabled) {
  color: var(--text-main);
  background: #f3f4f6;
}

button:disabled { opacity: 0.55; cursor: default; }

:where(button, input, textarea, select):focus-visible {
  outline: 2px solid var(--text-main);
  outline-offset: 2px;
}

.generator-controls {
  margin: 1rem 0.5rem 1rem 1rem;
  min-width: 0;
}

.generator-result {
  margin: 1rem 1rem 1rem 0.5rem;
}

@media (max-width: 899px) {
  .generator-controls,
  .generator-result {
    margin-inline: 1rem;
  }
}

.generator-layout.controls-hidden .generator-result {
  margin-inline: 1rem;
}

.scene-actions {
  display: flex;
  flex-wrap: wrap;
  flex-direction: column;
  gap: 0.5rem;
}

.scene-actions button:not(.primary) {
  border-color: var(--field-border);
  color: var(--text-main);
}

dialog {
  padding: 1.5rem;
  border: 1px solid var(--panel-border);
  border-radius: 4px;
  background: var(--panel-bg);
}

.section-heading {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.25rem 0.75rem;
  margin-bottom: 1rem;
}

.section-heading h2 {
  font-size: 1.2rem;
  font-weight: 600;
}

label {
  display: block;
  margin: 1rem 0 0.5rem;
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

input:not([type="checkbox"], [type="radio"]), textarea, select {
  width: 100%;
  min-width: 0;
  padding: 0.65rem 0.75rem;
  border: 1px solid var(--field-border);
  border-radius: 4px;
  background: var(--panel-bg);
  color: var(--text-main);
}

textarea {
  display: block;
  min-height: 5rem;
  resize: vertical;
}

.options {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.875rem;
}

#status, .preview-toolbar, .dialog-status {
  color: var(--text-muted);
  font-size: 0.85rem;
}

.field-heading {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  margin-top: 0.5rem;
}

.field-heading label { margin: 0; }

.character {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.375rem 0.625rem;
  margin-bottom: 0.5rem;
  border: 1px solid var(--panel-border);
  border-radius: 4px;
}

.scene-cast-label {
  margin-top: 0.5rem;
  font-weight: 600;
}

.scene-cast {
  padding: 0.375rem 0.625rem;
  border: 1px solid var(--panel-border);
  border-radius: 4px;
}

#scene-characters .character-edit {
  width: 100%;
  border: 0;
  border-radius: 0;
  background: transparent;
}

#scene-characters .character-edit + .character-edit {
  border-top: 1px solid var(--panel-border);
}

#add-scene-character {
  margin-top: 0.25rem;
}

.character-edit {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex: 1;
  min-width: 0;
  padding: 0.375rem 0.5rem;
  text-align: left;
}

.character-edit::after {
  content: "";
  flex: 0 0 0.4rem;
  height: 0.4rem;
  margin-right: 0.2rem;
  border: solid currentColor;
  border-width: 1px 1px 0 0;
  transform: rotate(45deg);
}

.character-edit > span { min-width: 0; }

.character-gender {
  display: flex;
  gap: 0.5rem;
  padding: 0;
  border: 0;
}

.character-gender label {
  position: relative;
  margin: 0;
}

.character-gender input {
  position: absolute;
  opacity: 0;
}

.character-gender span {
  display: grid;
  place-items: center;
  min-width: 2.5rem;
  min-height: 2.5rem;
  border: 1px solid var(--panel-border);
  border-radius: 4px;
  font-size: 1.25rem;
  cursor: pointer;
}

.character-gender input:checked + span {
  background: var(--text-main);
  color: white;
}

.character-gender input:focus-visible + span {
  outline: 2px solid var(--text-main);
  outline-offset: 2px;
}

.character-edit strong { display: block; color: var(--text-main); overflow-wrap: anywhere; }

.character-edit small {
  display: block;
  overflow: hidden;
  color: var(--text-muted);
  font-size: 0.75rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.character-edit small em { font-style: normal; }

.character input {
  flex: 0 0 auto;
  width: 1.1rem;
  height: 1.1rem;
  accent-color: var(--text-main);
}

.primary {
  padding: 0.75rem 1rem;
  border: 0;
  background: var(--text-main);
  color: #fff;
  font-weight: 700;
}

.primary:hover:not(:disabled) {
  background: var(--accent-secondary);
  color: #fff;
}

#generate { letter-spacing: 0.05em; }

#status { margin: 0.5rem 0 0; text-align: center; }
#status:empty, .dialog-status:empty { display: none; }

.preview {
  padding: 1rem clamp(1rem, 5vw, 3rem) 3rem;
  border: 1px solid var(--panel-border);
  background: var(--panel-bg);
}

.preview-toolbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 0.25rem 0.75rem;
  margin-bottom: 1.5rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--panel-border);
}

.preview-toolbar > div { display: flex; flex-wrap: wrap; gap: 0.25rem; }

/* A standard six-inch text area: action starts at the page's 1.5-inch
   margin, dialogue at 2.5 inches, and character cues at 3.7 inches.
   Percentages retain those proportions when the preview is narrower. */
#screenplay {
  max-width: 6in;
  margin: 0 auto;
  color: #000;
  font: 400 12pt/16pt var(--font-script);
  letter-spacing: normal;
  overflow-wrap: break-word;
}

.scene-heading {
  margin: 0 0 16pt;
  font: inherit;
  font-weight: 700;
  text-transform: uppercase;
}

.action { margin: 16pt 0; white-space: pre-line; }
.speech {
  width: 58.333333%;
  margin: 16pt 0 16pt 16.666667%;
}
.speaker {
  margin: 0 0 0 34.285714%;
  text-align: left;
  font-weight: 400;
  text-transform: uppercase;
}
.speech p { margin: 0; white-space: pre-line; }

dialog {
  width: min(32rem, calc(100% - 2rem));
  max-height: calc(100dvh - 2rem);
  color: var(--text-main);
}

dialog::backdrop { background: rgb(17 24 39 / 45%); }

.dialog-actions {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-top: 1.5rem;
}

.dialog-actions .primary { margin-left: auto; }
.close-dialog { min-width: 44px; padding: 0; font-size: 1.25rem; }

@media (max-width: 599px) {
  /* Keep the script at 12pt; relax only the indents on small screens. */
  .speech { width: 84%; margin-left: 8%; }
  .speaker { margin-left: 22%; }

  dialog {
    inset: auto 0 0;
    width: 100%;
    max-width: none;
    max-height: 90dvh;
    margin: 0;
    padding: 1rem 1rem max(1rem, env(safe-area-inset-bottom));
    border-radius: 12px 12px 0 0;
  }
}

#model[aria-busy="true"] {
  background-image: linear-gradient(110deg, transparent 35%, rgba(100, 116, 139, .12) 50%, transparent 65%);
  background-size: 250% 100%;
  animation: model-shimmer 1.6s ease-in-out infinite;
}

@keyframes model-shimmer {
  from { background-position: 100% 0; }
  to { background-position: 0 0; }
}

@media (prefers-reduced-motion: reduce) {
  #model[aria-busy="true"] { animation: none; }
}

.preview-toolbar #download {
  width: auto;
  min-height: 44px;
  margin: 0;
  padding: 0.5rem 0.65rem;
  border: 1px solid transparent;
  border-radius: 4px;
  color: var(--text-muted);
  background-color: transparent;
  font-size: 0.85rem;
  cursor: pointer;
}

.preview-toolbar #download:hover:not(:disabled) {
  color: var(--text-main);
  background-color: #f3f4f6;
}
