/* ==========================================================================
   NOLA Prompt Builder: a compact "tool panel" look (think Figma / After Effects).
   Dense on desktop; controls grow to touch size on phones (pointer: coarse).
   ========================================================================== */

:root {
  /* Surfaces: canvas sits behind panels; panels hold controls. */
  --canvas: #e6e6e6;
  --panel: #ffffff;
  --panel-raised: #f5f5f5;
  --input: #f0f0f0;
  --hairline: #e3e3e3;
  --border: #d4d4d4;
  --text: #1e1e1e;
  --muted: #737373;
  --faint: #858585;
  --chip: #ececec;
  --chip-hover: #e0e0e0;
  --accent: #0a72cc;        /* Text, focus rings, selected states. */
  --accent-fill: #0a72cc;   /* Filled buttons (white text needs >= 4.5:1). */
  --accent-text: #ffffff;
  --accent-soft: rgb(11 127 224 / 0.12);
  --success: #14a05a;
  --warn: #b26b00;
  --danger: #d8343a;
  --shadow: 0 10px 30px rgb(0 0 0 / 0.16), 0 0 0 1px rgb(0 0 0 / 0.06);

  /* Density */
  --ctl: 28px;          /* Buttons and inputs. */
  --chip-h: 22px;
  --fs: 12px;           /* UI text. */
  --fs-input: 13px;
  --fs-label: 11px;
  --pad: 12px;          /* Panel side padding. */
  --radius: 6px;
  --radius-sm: 4px;
  --top-height: 44px;
  --bar-height: 44px;

  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --canvas: #161616;
    --panel: #232323;
    --panel-raised: #2b2b2b;
    --input: #1b1b1b;
    --hairline: #313131;
    --border: #3d3d3d;
    --text: #e8e8e8;
    --muted: #9b9b9b;
    --faint: #7a7a7a;
    --chip: #333333;
    --chip-hover: #3d3d3d;
    --accent: #3d9eff;
    --accent-fill: #1670d0;
    --accent-text: #ffffff;
    --accent-soft: rgb(43 149 255 / 0.18);
    --success: #2fbf71;
    --warn: #f0b44c;
    --danger: #ff5f5f;
    --shadow: 0 12px 32px rgb(0 0 0 / 0.5), 0 0 0 1px rgb(255 255 255 / 0.06);
  }
}

/* Touch devices: same look, finger-sized controls; 16px inputs stop iOS zooming in. */
@media (pointer: coarse) {
  :root {
    --ctl: 40px;
    --chip-h: 30px;
    --fs: 14px;
    --fs-input: 16px;
    --fs-label: 12px;
    --bar-height: 60px;
  }
}

/* --------------------------------------------------------------------------
   Base
   -------------------------------------------------------------------------- */

* { box-sizing: border-box; scrollbar-width: thin; scrollbar-color: var(--border) transparent; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--canvas);
  color: var(--text);
  font: var(--fs)/1.4 Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  padding-bottom: calc(var(--bar-height) + env(safe-area-inset-bottom));
}

h1, h2 { margin: 0; }
h1 { font-size: 13px; font-weight: 600; white-space: nowrap; }
h2 { font-size: var(--fs-label); font-weight: 600; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

::selection { background: var(--accent-soft); }

/* --------------------------------------------------------------------------
   Controls
   -------------------------------------------------------------------------- */

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

/* Default button: flat "ghost" style that shows a surface on hover. */
button {
  min-height: var(--ctl);
  padding: 0 10px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.1s, border-color 0.1s, color 0.1s;
}
button:hover:not(:disabled) { background: var(--panel-raised); }
button:active:not(:disabled) { background: var(--chip-hover); }
button:disabled { color: var(--faint); cursor: default; }
button.primary {
  background: var(--accent-fill);
  color: var(--accent-text);
  font-weight: 600;
}
button.primary:hover:not(:disabled), button.primary:active:not(:disabled) {
  background: color-mix(in srgb, var(--accent-fill) 88%, white);
}
button.danger { color: var(--danger); }
button.icon { width: var(--ctl); padding: 0; color: var(--muted); }
button.icon:hover:not(:disabled) { color: var(--text); }
button.icon.danger:hover:not(:disabled) { color: var(--danger); }

/* Inputs: filled and borderless until hover/focus (Figma-style). */
input:not([type="checkbox"]), select, textarea {
  width: 100%;
  min-height: var(--ctl);
  padding: 4px 8px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: var(--input);
  font-size: var(--fs-input);
  transition: border-color 0.1s;
}
input:not([type="checkbox"]):hover, select:hover, textarea:hover { border-color: var(--border); }
input:not([type="checkbox"]):focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}
input::placeholder, textarea::placeholder { color: var(--faint); }
select { cursor: pointer; }
textarea {
  display: block;
  resize: vertical;
  field-sizing: content;
  min-height: calc(var(--fs-input) * 1.45 * 4 + 14px);
  padding: 6px 8px;
  line-height: 1.45;
}
input[type="checkbox"] { width: 14px; height: 14px; accent-color: var(--accent); margin: 0; }

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

label { font-weight: 600; }
.check {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: var(--ctl);
  font-weight: 400;
  color: var(--muted);
  white-space: nowrap;
  cursor: pointer;
}

/* --------------------------------------------------------------------------
   Top bar
   -------------------------------------------------------------------------- */

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 6px 16px;
  min-height: var(--top-height);
  padding: 6px var(--pad);
  padding-top: max(6px, env(safe-area-inset-top));
  background: var(--panel);
  border-bottom: 1px solid var(--hairline);
}
.toolbar { display: flex; flex-wrap: wrap; gap: 2px; }
.toolbar > button, .menu summary { color: var(--muted); }
.toolbar > button:hover, .menu summary:hover { color: var(--text); }
.toolbar button[aria-pressed="true"],
.toolbar button[aria-pressed="true"]:hover { background: var(--accent-soft); color: var(--accent); }
@media (max-width: 480px) {
  .toolbar { display: grid; grid-template-columns: repeat(4, 1fr); width: 100%; }
  .toolbar > button, .menu summary { justify-content: center; padding: 0 4px; }
}

.menu { position: relative; }
.menu summary {
  list-style: none;
  display: flex;
  align-items: center;
  min-height: var(--ctl);
  padding: 0 10px;
  border-radius: var(--radius-sm);
  font-weight: 500;
  cursor: pointer;
}
.menu summary:hover, .menu[open] summary { background: var(--panel-raised); }
.menu summary::-webkit-details-marker { display: none; }
.menu summary::after {
  content: "";
  margin-left: 6px;
  border: 4px solid transparent;
  border-top-color: currentColor;
  transform: translateY(2px);
}
.menu-items {
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  z-index: 10;
  display: flex;
  flex-direction: column;
  min-width: 168px;
  padding: 4px;
  border-radius: var(--radius);
  background: var(--panel-raised);
  box-shadow: var(--shadow);
}
.menu-items button { text-align: left; font-weight: 400; }
.menu-items button:hover:not(:disabled) { background: var(--accent-fill); color: var(--accent-text); }

/* --------------------------------------------------------------------------
   Layout: inspector panel (fields) + canvas (preview)
   -------------------------------------------------------------------------- */

.layout { display: grid; }
.form-col { display: flex; flex-direction: column; min-width: 0; background: var(--panel); }
.preview-col { min-width: 0; }

/* Wide screens: app-style split, each side scrolls on its own. */
@media (min-width: 900px) {
  .layout {
    grid-template-columns: minmax(340px, 420px) minmax(0, 1fr);
    height: calc(100dvh - var(--top-height) - var(--bar-height));
  }
  .form-col { overflow-y: auto; border-right: 1px solid var(--hairline); }
  .preview-col { overflow-y: auto; padding: 24px; }
  .preview-col > * { max-width: 760px; margin-left: auto; margin-right: auto; }
}

/* A section of the inspector: flat, divided by hairlines. */
.card {
  padding: 10px var(--pad);
  background: var(--panel);
  border-bottom: 1px solid var(--hairline);
}
.empty { color: var(--muted); margin: 0; }

.presets { display: grid; grid-template-columns: 1fr auto auto; align-items: center; gap: 4px; }

/* --------------------------------------------------------------------------
   Fields
   -------------------------------------------------------------------------- */

.field { display: flex; flex-direction: column; gap: 6px; }
.field-head { display: flex; align-items: center; gap: 4px; }
.field-head > label:not(.check) { font-size: var(--fs-label); font-weight: 600; }
.label-input { flex: 1 1 80px; min-width: 0; width: auto; font-weight: 600; }
.field-head .check { font-size: var(--fs-label); margin: 0 4px; }
.head-buttons { display: flex; margin-left: auto; }

.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip { display: inline-flex; max-width: 100%; }
.chip button, .chip-save {
  min-height: var(--chip-h);
  padding: 0 8px;
  font-size: var(--fs-label);
  font-weight: 400;
  border-radius: var(--radius-sm);
}
.chip-pick {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  background: var(--chip);
}
.chip-pick:hover:not(:disabled) { background: var(--chip-hover); }
.chip:has(.chip-remove) .chip-pick { border-radius: var(--radius-sm) 0 0 var(--radius-sm); }
.chip-remove {
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  border-left: 1px solid var(--panel);
  background: var(--chip);
  color: var(--muted);
}
.chip-remove:hover:not(:disabled) { background: var(--chip-hover); color: var(--danger); }
.chip-save {
  border: 1px dashed var(--border);
  color: var(--muted);
  white-space: nowrap;
}
.chip-save:hover:not(:disabled) { color: var(--text); border-color: var(--muted); background: transparent; }

/* Narrow screens: chips become one swipeable row instead of wrapping onto many lines. */
@media (max-width: 600px) {
  .chips {
    flex-wrap: nowrap;
    overflow-x: auto;
    margin: 0 calc(var(--pad) * -1);
    padding: 0 var(--pad);
    scrollbar-width: none;
    overscroll-behavior-x: contain;
  }
  .chips::-webkit-scrollbar { display: none; }
  .chip { flex: none; max-width: 80%; }
  .chip-save { order: -1; flex: none; }
}

.add-field { display: flex; flex-direction: column; gap: 6px; }
.add-field > label { font-size: var(--fs-label); }
.add-row { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.add-row input:not([type="checkbox"]) { flex: 1 1 160px; width: auto; }
.add-row button[type="submit"] { background: var(--chip); }
.add-row button[type="submit"]:hover { background: var(--chip-hover); }

/* --------------------------------------------------------------------------
   Preview (the "canvas")
   -------------------------------------------------------------------------- */

.preview-head { padding: 12px var(--pad) 6px; }
.preview-head h2 { color: var(--muted); }
#preview {
  margin: 0;
  min-height: 120px;
  padding: 12px var(--pad);
  background: var(--panel);
  font: 12px/1.6 var(--mono);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
#preview:empty::before { content: attr(data-empty); color: var(--faint); }
@media (pointer: coarse) {
  #preview { font-size: 13px; }
}
@media (min-width: 900px) {
  .preview-head { padding: 0 2px 8px; }
  #preview {
    min-height: 240px;
    padding: 20px 24px;
    border-radius: var(--radius);
    box-shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 0 0 1px var(--hairline);
  }
}

/* --------------------------------------------------------------------------
   Status bar: character count + Copy, always within thumb reach
   -------------------------------------------------------------------------- */

.actionbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  min-height: calc(var(--bar-height) + env(safe-area-inset-bottom));
  padding: 0 var(--pad) env(safe-area-inset-bottom);
  background: var(--panel);
  border-top: 1px solid var(--hairline);
}
.count {
  margin-right: auto;
  color: var(--muted);
  font: 11px var(--mono);
  font-variant-numeric: tabular-nums;
}
.count.near { color: var(--warn); }
.count.over { color: var(--danger); font-weight: 700; }
#copy-btn { min-width: 120px; }
@media (pointer: coarse) {
  #copy-btn { min-width: 150px; min-height: 44px; font-size: 15px; border-radius: var(--radius); }
}
#copy-btn.copied { background: var(--success); }

/* --------------------------------------------------------------------------
   Toast
   -------------------------------------------------------------------------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--bar-height) + 10px + env(safe-area-inset-bottom));
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 8px;
  width: max-content;
  max-width: calc(100% - 24px);
  padding: 4px 4px 4px 12px;
  border-radius: var(--radius);
  background: var(--panel-raised);
  box-shadow: var(--shadow);
  transform: translate(-50%, 12px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s, transform 0.15s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
.toast:not(:has(button)) { padding: 8px 12px; }
.toast button { color: var(--accent); font-weight: 600; }

/* --------------------------------------------------------------------------
   Dialogs
   -------------------------------------------------------------------------- */

dialog {
  max-width: min(400px, calc(100% - 24px));
  padding: 16px;
  border: 0;
  border-radius: var(--radius);
  background: var(--panel-raised);
  color: var(--text);
  box-shadow: var(--shadow);
}
dialog h2 { font-size: 13px; }
dialog::backdrop { background: rgb(0 0 0 / 0.4); }
dialog p { color: var(--muted); }
.dialog-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 4px; }
.dialog-actions button:not(.primary) { background: var(--chip); }
.dialog-actions button:not(.primary):hover { background: var(--chip-hover); }

/* History panel: full screen on phones, a right-hand sheet on wider screens.
   No padding on the dialog itself so a click whose target is the dialog means the backdrop. */
dialog.panel {
  width: 100%;
  max-width: 100%;
  height: 100%;
  max-height: 100%;
  margin: 0;
  padding: 0;
  border-radius: 0;
  background: var(--panel);
}
dialog.panel[open] { display: flex; flex-direction: column; }
@media (min-width: 600px) {
  dialog.panel {
    width: 380px;
    margin: 0 0 0 auto;
    border-left: 1px solid var(--hairline);
    animation: panel-in 0.16s ease-out;
  }
  dialog.panel::backdrop { background: rgb(0 0 0 / 0.25); }
}
@keyframes panel-in { from { transform: translateX(24px); opacity: 0; } }

.panel-head {
  display: flex;
  align-items: center;
  gap: 2px;
  min-height: var(--top-height);
  padding: 6px 6px 6px var(--pad);
  padding-top: max(6px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--hairline);
}
.panel-head h2 { margin-right: auto; font-size: 13px; }
.panel-note { margin: 0; padding: 10px var(--pad) 4px; color: var(--muted); font-size: var(--fs-label); }

.history-list {
  flex: 1;
  overflow-y: auto;
  margin: 0;
  padding: 4px 6px max(12px, env(safe-area-inset-bottom));
  list-style: none;
  display: flex;
  flex-direction: column;
}
.history-list .empty { padding: 6px; }
.history-item { display: flex; align-items: flex-start; border-radius: var(--radius-sm); }
.history-item:hover { background: var(--panel-raised); }
.history-item + .history-item { border-top: 1px solid var(--hairline); }
.history-load {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 8px 6px;
  text-align: left;
  font-weight: 400;
}
.history-load:hover:not(:disabled) { background: transparent; }
.history-time { font: 10px var(--mono); color: var(--muted); }
.history-title, .history-snippet { width: 100%; overflow: hidden; }
.history-title { font-weight: 600; white-space: nowrap; text-overflow: ellipsis; }
.history-snippet {
  color: var(--muted);
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}
/* Remove buttons appear on hover with a mouse; always visible on touch. */
.history-remove { margin-top: 6px; opacity: 0; }
.history-item:hover .history-remove, .history-remove:focus-visible { opacity: 1; }
@media (pointer: coarse) {
  .history-remove { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  *, ::backdrop { transition: none !important; animation: none !important; }
}
