/* ============================================================
   LivePad — Live Preview IDE
   Design system + responsive layout
   All application styles are scoped under .app-shell / .app-*
   to keep them isolated from user code (user code runs only
   inside the sandboxed preview iframe).
   ============================================================ */

/* ---------- Design tokens ---------- */
:root {
  color-scheme: light dark;

  /* spacing scale */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 28px;

  /* radii */
  --radius-sm: 6px;
  --radius-md: 9px;
  --radius-lg: 14px;

  /* fixed region heights (adaptive) */
  --header-height: clamp(46px, 5vh, 54px);
  --toolbar-height: 48px;
  --statusbar-height: 26px;
  --mobile-tabs-height: 44px;

  /* typography */
  --font-ui: "Inter", ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-mono: "JetBrains Mono", "Fira Code", "SFMono-Regular", ui-monospace, Consolas, "Liberation Mono", monospace;
  --fs-ui: clamp(0.8125rem, 0.78rem + 0.18vw, 0.9375rem);
  --fs-sm: clamp(0.72rem, 0.7rem + 0.12vw, 0.8125rem);

  --editor-font-size: 14px;
  --editor-tab-size: 2;

  /* motion */
  --ease: cubic-bezier(0.2, 0.6, 0.2, 1);
  --dur: 160ms;

  --shadow-1: 0 1px 2px rgba(0,0,0,.18);
  --shadow-2: 0 8px 30px rgba(0,0,0,.28);
  --z-popover: 60;
  --z-drawer: 80;
  --z-modal: 90;
  --z-toast: 100;
}

/* Dark theme (default) */
[data-theme="dark"] {
  --app-bg: #0d1117;
  --app-surface: #12181f;
  --app-surface-2: #161d26;
  --app-surface-muted: #0a0e13;
  --app-border: #232c38;
  --app-border-strong: #2f3a49;
  --app-text: #e6edf3;
  --app-text-muted: #8b98a9;
  --app-accent: #4f9dff;
  --app-accent-contrast: #06121f;
  --app-success: #3fb950;
  --app-warning: #d29922;
  --app-danger: #f85149;
  --app-hover: rgba(255,255,255,.06);
  --app-active: rgba(255,255,255,.1);
  --editor-bg: #0f151c;
  --editor-gutter: #0b1117;
  --editor-line: rgba(255,255,255,.04);
}

/* Light theme */
[data-theme="light"] {
  --app-bg: #f4f6f9;
  --app-surface: #ffffff;
  --app-surface-2: #f0f3f7;
  --app-surface-muted: #e9edf2;
  --app-border: #dbe1ea;
  --app-border-strong: #c4ccd8;
  --app-text: #1b2431;
  --app-text-muted: #5c6773;
  --app-accent: #1f6feb;
  --app-accent-contrast: #ffffff;
  --app-success: #1a7f37;
  --app-warning: #9a6700;
  --app-danger: #cf222e;
  --app-hover: rgba(0,0,0,.05);
  --app-active: rgba(0,0,0,.09);
  --editor-bg: #ffffff;
  --editor-gutter: #f4f6f9;
  --editor-line: rgba(0,0,0,.035);
}

/* ---------- Base reset (scoped-safe) ---------- */
*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font-ui);
  font-size: var(--fs-ui);
  line-height: 1.45;
  color: var(--app-text);
  background: var(--app-bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overscroll-behavior: none;
}

.app-shell button { font-family: inherit; }

.app-skip-link {
  position: absolute;
  left: -999px;
  top: 0;
  z-index: 200;
  background: var(--app-accent);
  color: var(--app-accent-contrast);
  padding: var(--space-2) var(--space-3);
  border-radius: 0 0 var(--radius-sm) 0;
}
.app-skip-link:focus { left: 0; }

/* ============================================================
   APP SHELL — grid: header / toolbar / mobile-tabs / workspace / statusbar
   ============================================================ */
.app-shell {
  height: 100dvh;
  height: 100svh;
  width: 100%;
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto auto 0 minmax(0, 1fr) auto;
  background: var(--app-bg);
  padding-top: env(safe-area-inset-top);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
  padding-bottom: env(safe-area-inset-bottom);
}
/* fallback for browsers without dvh */
@supports not (height: 100dvh) {
  .app-shell { height: 100vh; }
}

/* Explicit grid-row placement so hiding .app-mobile-tabs (display:none on
   desktop) never shifts the workspace into the wrong (0-height) track. */
.app-header { grid-row: 1; }
.app-toolbar { grid-row: 2; }
.app-mobile-tabs { grid-row: 3; }
.app-workspace { grid-row: 4; }
.app-statusbar { grid-row: 5; }

/* ---------- Shared control primitives ---------- */
.app-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  height: 34px;
  padding: 0 var(--space-3);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--app-text);
  font-size: var(--fs-sm);
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease), color var(--dur) var(--ease);
}
.app-btn i { font-size: 13px; width: 16px; text-align: center; flex: none; }
.app-btn:hover { background: var(--app-hover); }
.app-btn:active { background: var(--app-active); }
.app-btn--ghost { color: var(--app-text-muted); }
.app-btn--ghost:hover { color: var(--app-text); }
.app-btn--primary {
  background: var(--app-accent);
  color: var(--app-accent-contrast);
  font-weight: 600;
}
.app-btn--primary:hover { filter: brightness(1.08); background: var(--app-accent); }

.app-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--app-text-muted);
  cursor: pointer;
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease);
}
.app-icon-btn i { font-size: 15px; }
.app-icon-btn:hover { background: var(--app-hover); color: var(--app-text); }

/* Font Awesome icon defaults */
.app-logo i { font-size: 18px; }
.app-menu-trigger i, .app-preview-bar__label i { font-size: 13px; }
.app-popover__item i { width: 16px; text-align: center; margin-right: 6px; opacity: .85; }
.app-drawer__head h2 i, .app-modal__head h2 i { margin-right: 6px; color: var(--app-accent); }
.app-btn.is-running { opacity: .7; transform: scale(.97); }

/* (Icons are provided by Font Awesome; no inline SVG styling needed.) */

:where(button, select, input, [tabindex], a):focus-visible {
  outline: 2px solid var(--app-accent);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

/* ============================================================
   HEADER
   ============================================================ */
.app-header {
  height: var(--header-height);
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 0 var(--space-3);
  background: var(--app-surface);
  border-bottom: 1px solid var(--app-border);
  min-width: 0;
}
.app-header__left {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
}
.app-logo { display: inline-flex; color: var(--app-accent); }
.app-logo i { width: 22px; text-align: center; }
.app-title {
  font-weight: 700;
  letter-spacing: .2px;
  font-size: 0.95rem;
}
.app-project-name {
  border: 1px solid transparent;
  background: transparent;
  color: var(--app-text-muted);
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-sm);
  cursor: pointer;
  max-width: 40vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: var(--fs-sm);
}
.app-project-name:hover { background: var(--app-hover); color: var(--app-text); }
.app-project-name::before { content: "/ "; opacity: .5; }

.app-header__menus {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  margin-left: var(--space-2);
}
.app-menu-trigger {
  border: none;
  background: transparent;
  color: var(--app-text-muted);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: var(--fs-sm);
}
.app-menu-trigger:hover, .app-menu-trigger[aria-expanded="true"] {
  background: var(--app-hover);
  color: var(--app-text);
}
.app-header__right { margin-left: auto; display: flex; align-items: center; gap: var(--space-2); }
.app-header__menu { display: none; }

/* ============================================================
   TOOLBAR
   ============================================================ */
.app-toolbar {
  height: var(--toolbar-height);
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: 0 var(--space-3);
  background: var(--app-surface-2);
  border-bottom: 1px solid var(--app-border);
  min-width: 0;
  overflow: hidden;
}
.app-toolbar__group { display: flex; align-items: center; gap: var(--space-1); min-width: 0; }
.app-toolbar__divider { width: 1px; height: 22px; background: var(--app-border); flex: none; }
.app-toolbar__spacer { flex: 1 1 auto; min-width: 0; }
.app-toolbar__more { display: none; }

/* live switch */
.app-switch { display: inline-flex; align-items: center; gap: var(--space-2); cursor: pointer; user-select: none; }
.app-switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.app-switch__track {
  width: 34px; height: 18px;
  background: var(--app-border-strong);
  border-radius: 999px;
  position: relative;
  transition: background var(--dur) var(--ease);
  flex: none;
}
.app-switch__thumb {
  position: absolute; top: 2px; left: 2px;
  width: 14px; height: 14px;
  border-radius: 50%;
  background: #fff;
  transition: transform var(--dur) var(--ease);
}
.app-switch input:checked + .app-switch__track { background: var(--app-success); }
.app-switch input:checked + .app-switch__track .app-switch__thumb { transform: translateX(16px); }
.app-switch input:focus-visible + .app-switch__track { outline: 2px solid var(--app-accent); outline-offset: 2px; }
.app-switch__label { font-size: var(--fs-sm); color: var(--app-text-muted); }

/* selects */
.app-select {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  height: 34px;
  padding: 0 var(--space-2);
  border: 1px solid var(--app-border);
  border-radius: var(--radius-sm);
  background: var(--app-surface);
  color: var(--app-text);
}
.app-select__icon { display: inline-flex; color: var(--app-text-muted); }
.app-select__icon i { font-size: 13px; }
.app-select select {
  border: none;
  background: transparent;
  color: inherit;
  font-size: var(--fs-sm);
  cursor: pointer;
  outline: none;
  padding: 0 var(--space-1);
}
.app-select select option { background: var(--app-surface); color: var(--app-text); }

/* ============================================================
   MOBILE WORKSPACE TABS (hidden on desktop)
   ============================================================ */
.app-mobile-tabs { display: none; }

/* ============================================================
   WORKSPACE
   ============================================================ */
.app-workspace {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: row;
  background: var(--app-bg);
  position: relative;
}
.app-workspace[data-direction="vertical"] { flex-direction: column; }

/* editor + preview panels */
.app-editors,
.app-preview-wrap {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  flex: 1 1 0;
}
/* split sizing driven by --split variable (percentage for editors) */
.app-workspace[data-direction="horizontal"] .app-editors { flex: 0 0 var(--split, 50%); }
.app-workspace[data-direction="horizontal"] .app-preview-wrap { flex: 1 1 0; }
.app-workspace[data-direction="vertical"] .app-editors { flex: 0 0 var(--split, 50%); }
.app-workspace[data-direction="vertical"] .app-preview-wrap { flex: 1 1 0; }

/* ---------- Editor tabs ---------- */
.app-editors { background: var(--editor-bg); border-right: 1px solid var(--app-border); }
.app-workspace[data-direction="vertical"] .app-editors { border-right: none; border-bottom: 1px solid var(--app-border); }

.app-editor-tabs {
  display: flex;
  align-items: stretch;
  gap: 2px;
  padding: var(--space-2) var(--space-2) 0;
  background: var(--app-surface-2);
  border-bottom: 1px solid var(--app-border);
  flex: none;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: none;
}
.app-editor-tabs::-webkit-scrollbar { display: none; }
.app-editor-tab {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  border: none;
  background: transparent;
  color: var(--app-text-muted);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
  cursor: pointer;
  font-size: var(--fs-sm);
  white-space: nowrap;
  border-bottom: 2px solid transparent;
}
.app-editor-tab:hover { color: var(--app-text); background: var(--app-hover); }
.app-editor-tab.is-active {
  color: var(--app-text);
  background: var(--editor-bg);
  border-bottom-color: var(--app-accent);
}
.app-editor-tab i { font-size: 12px; opacity: .9; }
.app-editor-tab[data-lang="html"] i { color: #e34c26; }
.app-editor-tab[data-lang="css"] i { color: #2965f1; }
.app-editor-tab[data-lang="js"] i { color: #f0db4f; }
.app-editor-tabs__action { margin-left: auto; }

.app-editor-stack {
  position: relative;
  flex: 1 1 0;
  min-height: 0;
  min-width: 0;
  display: flex;
}
.app-editor-pane {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  display: none;
}
.app-editor-pane.is-active { display: flex; }

/* ---------- CodeMirror 6 integration ---------- */
.app-editor-pane .cm-editor {
  flex: 1 1 auto;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  background: var(--editor-bg);
  font-family: var(--font-mono);
}
.app-editor-pane .cm-scroller { font-family: var(--font-mono); line-height: 1.55; overflow: auto; }
.app-editor-pane .cm-editor.cm-focused { outline: none; }
.app-editor-pane .cm-gutters { background: var(--editor-gutter); border-right: 1px solid var(--app-border); color: var(--app-text-muted); }
/* Light theme (dark uses the one-dark extension) */
[data-theme="light"] .app-editor-pane .cm-content { color: var(--app-text); }
[data-theme="light"] .app-editor-pane .cm-activeLine { background: var(--editor-line); }
[data-theme="light"] .app-editor-pane .cm-activeLineGutter { background: var(--editor-line); }
[data-theme="light"] .app-editor-pane .cm-selectionBackground,
[data-theme="light"] .app-editor-pane .cm-selectionBackground {
  background: color-mix(in srgb, var(--app-accent) 22%, transparent) !important;
}

/* ---------- Textarea fallback editor widget ---------- */
.ed {
  display: flex;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  background: var(--editor-bg);
  font-family: var(--font-mono);
  font-size: var(--editor-font-size);
  line-height: 1.55;
  position: relative;
  overflow: hidden;
}
.ed__scroll {
  display: flex;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  overflow: auto;
  position: relative;
}
.ed__gutter {
  flex: none;
  padding: var(--space-2) var(--space-2);
  text-align: right;
  color: var(--app-text-muted);
  background: var(--editor-gutter);
  user-select: none;
  border-right: 1px solid var(--app-border);
  position: sticky;
  left: 0;
  z-index: 1;
  white-space: pre;
  font-variant-numeric: tabular-nums;
  opacity: .8;
}
.ed--no-gutter .ed__gutter { display: none; }
.ed__area {
  flex: 1 1 auto;
  min-width: 0;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--app-text);
  font: inherit;
  line-height: inherit;
  padding: var(--space-2) var(--space-3);
  tab-size: var(--editor-tab-size);
  -moz-tab-size: var(--editor-tab-size);
  white-space: pre;
  overflow: hidden;
  spellcheck: false;
  caret-color: var(--app-accent);
}
.ed--wrap .ed__area { white-space: pre-wrap; word-break: break-word; }
.ed__area::placeholder { color: var(--app-text-muted); }
.ed__area::selection { background: color-mix(in srgb, var(--app-accent) 35%, transparent); }

/* ---------- Resizer ---------- */
.app-resizer {
  flex: 0 0 auto;
  position: relative;
  z-index: 5;
  background: var(--app-border);
  transition: background var(--dur) var(--ease);
}
.app-workspace[data-direction="horizontal"] .app-resizer {
  width: 6px; cursor: col-resize; margin: 0 -3px;
}
.app-workspace[data-direction="vertical"] .app-resizer {
  height: 6px; cursor: row-resize; margin: -3px 0;
}
.app-resizer:hover, .app-resizer.is-dragging, .app-resizer:focus-visible { background: var(--app-accent); }
.app-resizer__grip {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  background: var(--app-border-strong);
  border-radius: 3px;
}
.app-workspace[data-direction="horizontal"] .app-resizer__grip { width: 3px; height: 28px; }
.app-workspace[data-direction="vertical"] .app-resizer__grip { width: 28px; height: 3px; }

/* ============================================================
   PREVIEW
   ============================================================ */
.app-preview-wrap { background: var(--app-surface-muted); }
.app-preview-bar {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  height: 38px;
  padding: 0 var(--space-2) 0 var(--space-3);
  background: var(--app-surface-2);
  border-bottom: 1px solid var(--app-border);
  flex: none;
}
.app-preview-bar__label { font-size: var(--fs-sm); font-weight: 600; color: var(--app-text-muted); }
.app-preview-bar__dims { font-size: var(--fs-sm); color: var(--app-text-muted); font-variant-numeric: tabular-nums; }

.app-preview-stage {
  flex: 1 1 0;
  min-height: 0;
  min-width: 0;
  overflow: auto;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 0;
  background:
    repeating-conic-gradient(var(--app-surface-muted) 0% 25%, transparent 0% 50%) 0 0 / 22px 22px;
}
.app-preview-stage[data-device="responsive"] { padding: 0; }
.app-preview-stage[data-device]:not([data-device="responsive"]) { padding: var(--space-4); }

.app-preview-viewport {
  position: relative;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  background: #fff;
  transform-origin: top center;
  transition: width var(--dur) var(--ease);
}
.app-preview-stage[data-device]:not([data-device="responsive"]) .app-preview-viewport {
  height: auto;
  min-height: 100%;
  box-shadow: var(--shadow-2);
  border-radius: var(--radius-sm);
  overflow: hidden;
}
.app-preview-frame {
  width: 100%;
  height: 100%;
  min-height: 100%;
  border: 0;
  display: block;
  background: #fff;
}

/* ============================================================
   CONSOLE
   ============================================================ */
.app-console {
  flex: none;
  display: flex;
  flex-direction: column;
  border-top: 1px solid var(--app-border);
  background: var(--app-surface);
  min-height: 0;
}
.app-console[data-open="true"] {
  height: clamp(120px, 30vh, 260px);
}
.app-console[data-open="true"] .app-console__log { display: block; }
.app-console__bar {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  height: 32px;
  padding: 0 var(--space-2);
  flex: none;
}
.app-console__toggle {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  border: none;
  background: transparent;
  color: var(--app-text-muted);
  cursor: pointer;
  font-size: var(--fs-sm);
  font-weight: 600;
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-sm);
}
.app-console__toggle:hover { background: var(--app-hover); color: var(--app-text); }
.app-console__toggle .chev { font-size: 11px; transition: transform var(--dur) var(--ease); transform: rotate(180deg); }
.app-console[data-open="true"] .chev { transform: rotate(0deg); }
.app-console__log {
  display: none;
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  padding: var(--space-1) 0;
  font-family: var(--font-mono);
  font-size: 12.5px;
  line-height: 1.5;
}
.app-console__row {
  display: flex;
  gap: var(--space-2);
  padding: 3px var(--space-3);
  border-bottom: 1px solid var(--editor-line);
  white-space: pre-wrap;
  word-break: break-word;
}
.app-console__row--error { background: color-mix(in srgb, var(--app-danger) 12%, transparent); color: var(--app-danger); }
.app-console__row--warn { background: color-mix(in srgb, var(--app-warning) 12%, transparent); color: var(--app-warning); }
.app-console__row--info { color: var(--app-text-muted); }
.app-console__row-badge { flex: none; opacity: .6; user-select: none; }

.app-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: 999px;
  background: var(--app-danger);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
}

/* ============================================================
   STATUS BAR
   ============================================================ */
.app-statusbar {
  height: var(--statusbar-height);
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 0 var(--space-3);
  background: var(--app-surface);
  border-top: 1px solid var(--app-border);
  font-size: var(--fs-sm);
  color: var(--app-text-muted);
  min-width: 0;
  overflow: hidden;
}
.app-status { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.app-status__dot { width: 8px; height: 8px; border-radius: 50%; background: var(--app-success); }
.app-status--save .app-status__ico { color: var(--app-success); }
.app-status--save[data-dirty="true"] .app-status__ico { color: var(--app-warning); }
.app-status--live .app-status__ico { color: var(--app-success); }
.app-status--live[data-off="true"] .app-status__ico { color: var(--app-text-muted); }
.app-status--secondary { color: var(--app-text-muted); font-variant-numeric: tabular-nums; }
.app-status--engine { color: var(--app-accent); }

/* ============================================================
   POPOVERS / MENUS
   ============================================================ */
.app-popover {
  position: fixed;
  z-index: var(--z-popover);
  min-width: 190px;
  max-width: min(90vw, 280px);
  background: var(--app-surface);
  border: 1px solid var(--app-border-strong);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-2);
  padding: var(--space-1);
  animation: pop-in var(--dur) var(--ease);
}
@keyframes pop-in { from { opacity: 0; transform: translateY(-4px); } }
.app-popover__item {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  color: var(--app-text);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: var(--fs-sm);
  white-space: nowrap;
}
.app-popover__item:hover { background: var(--app-hover); }
.app-popover__sep { height: 1px; background: var(--app-border); margin: var(--space-1) var(--space-2); }

/* ============================================================
   OVERLAY / DRAWER / MODAL
   ============================================================ */
.app-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  z-index: var(--z-drawer);
  animation: fade-in var(--dur) var(--ease);
}
@keyframes fade-in { from { opacity: 0; } }

.app-drawer {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: min(420px, 100vw);
  z-index: var(--z-drawer);
  background: var(--app-surface);
  border-left: 1px solid var(--app-border);
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-2);
  animation: slide-in var(--dur) var(--ease);
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}
@keyframes slide-in { from { transform: translateX(100%); } }
.app-drawer__head {
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--app-border);
  flex: none;
}
.app-drawer__head h2 { margin: 0; font-size: 1rem; }
.app-drawer__body { padding: var(--space-4); overflow: auto; flex: 1 1 auto; min-height: 0; }
.app-drawer__actions { margin-top: var(--space-4); }

.app-field { margin-bottom: var(--space-4); display: flex; flex-direction: column; gap: var(--space-2); }
.app-field label { font-size: var(--fs-sm); color: var(--app-text-muted); font-weight: 600; }
.app-field--row { flex-direction: row; align-items: center; justify-content: space-between; }
.app-field select, .app-field textarea, .app-input {
  width: 100%;
  background: var(--app-surface-2);
  color: var(--app-text);
  border: 1px solid var(--app-border);
  border-radius: var(--radius-sm);
  padding: var(--space-2) var(--space-3);
  font-family: inherit;
  font-size: var(--fs-sm);
}
.app-field textarea { font-family: var(--font-mono); resize: vertical; }
.app-field input[type="range"] { width: 100%; accent-color: var(--app-accent); }
.app-field input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--app-accent); }

/* modals */
.app-modal-wrap {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal);
  display: grid;
  place-items: center;
  padding: var(--space-4);
  background: rgba(0,0,0,.5);
  animation: fade-in var(--dur) var(--ease);
}
.app-modal {
  width: min(640px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  background: var(--app-surface);
  border: 1px solid var(--app-border-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-2);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: pop-in var(--dur) var(--ease);
}
.app-modal--sm { width: min(420px, calc(100vw - 24px)); }
.app-modal__head {
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--app-border);
  flex: none;
}
.app-modal__head h2 { margin: 0; font-size: 1rem; }
.app-modal__body { padding: var(--space-4); overflow: auto; min-height: 0; }
.app-modal__foot {
  display: flex; justify-content: flex-end; gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  border-top: 1px solid var(--app-border);
  flex: none;
}

/* templates grid */
#templatesGrid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: var(--space-3);
}
.tpl-card {
  border: 1px solid var(--app-border);
  border-radius: var(--radius-md);
  background: var(--app-surface-2);
  padding: var(--space-3);
  cursor: pointer;
  text-align: left;
  color: var(--app-text);
  transition: border-color var(--dur) var(--ease), transform var(--dur) var(--ease);
}
.tpl-card:hover { border-color: var(--app-accent); transform: translateY(-2px); }
.tpl-card__icon { font-size: 1.5rem; color: var(--app-accent); }
.tpl-card__name { font-weight: 600; margin-top: var(--space-2); }
.tpl-card__desc { font-size: var(--fs-sm); color: var(--app-text-muted); margin-top: 2px; }

/* settings drawer group headings */
.app-drawer__group {
  font-size: var(--fs-sm);
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--app-text-muted);
  margin: var(--space-4) 0 var(--space-2);
  padding-bottom: var(--space-1);
  border-bottom: 1px solid var(--app-border);
}
.app-drawer__group:first-child { margin-top: 0; }

/* status bar icons */
.app-status__ico { font-size: 11px; }

/* ============================================================
   TOASTS
   ============================================================ */
.app-toasts {
  position: fixed;
  z-index: var(--z-toast);
  left: 50%;
  bottom: calc(var(--statusbar-height) + 12px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  width: max-content;
  max-width: min(92vw, 420px);
  pointer-events: none;
}
.app-toast {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  background: var(--app-surface);
  color: var(--app-text);
  border: 1px solid var(--app-border-strong);
  border-left: 3px solid var(--app-accent);
  border-radius: var(--radius-md);
  padding: var(--space-2) var(--space-3);
  box-shadow: var(--shadow-2);
  font-size: var(--fs-sm);
  animation: toast-in var(--dur) var(--ease);
}
.app-toast--success { border-left-color: var(--app-success); }
.app-toast--error { border-left-color: var(--app-danger); }
.app-toast--warn { border-left-color: var(--app-warning); }
.app-toast.is-leaving { animation: toast-out var(--dur) var(--ease) forwards; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }
@keyframes toast-out { to { opacity: 0; transform: translateY(8px); } }

/* ============================================================
   FULLSCREEN MODES
   ============================================================ */
.app-shell[data-fs="preview"] .app-editors,
.app-shell[data-fs="preview"] .app-resizer { display: none; }
.app-shell[data-fs="editor"] .app-preview-wrap,
.app-shell[data-fs="editor"] .app-resizer { display: none; }

/* ============================================================
   RESPONSIVE — TABLET (compact split)
   ============================================================ */
@media (max-width: 1023px) {
  .app-header__menus { display: none; }
  .app-btn__label { display: none; }
  .app-btn { padding: 0 var(--space-2); }
  .app-btn--primary .app-btn__label { display: inline; }
  .app-toolbar__preview-tools .app-select__icon { display: none; }
}

/* ============================================================
   RESPONSIVE — MOBILE (tabbed workspace)
   ============================================================ */
@media (max-width: 767px) {
  :root { --toolbar-height: 46px; }

  .app-shell {
    grid-template-rows: auto auto var(--mobile-tabs-height) minmax(0, 1fr) auto;
  }

  .app-header__menu { display: inline-flex; }
  .app-project-name { display: none; }
  .app-title { font-size: 0.9rem; }

  /* collapse most toolbar actions into More menu */
  .app-toolbar__group[data-collapse="secondary"] { display: none; }
  .app-toolbar__preview-tools { display: none; }
  .app-toolbar__more { display: inline-flex; }
  .app-toolbar__more .app-btn__label { display: none; }

  /* mobile workspace tabs */
  .app-mobile-tabs {
    display: flex;
    align-items: stretch;
    height: var(--mobile-tabs-height);
    background: var(--app-surface-2);
    border-bottom: 1px solid var(--app-border);
    padding: 0 var(--space-2);
    gap: var(--space-1);
  }
  .app-mobile-tab {
    flex: 1 1 0;
    border: none;
    background: transparent;
    color: var(--app-text-muted);
    font-size: var(--fs-sm);
    font-weight: 600;
    cursor: pointer;
    border-bottom: 2px solid transparent;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-height: 44px;
  }
  .app-mobile-tab.is-active { color: var(--app-text); border-bottom-color: var(--app-accent); }

  /* workspace becomes a single-view container; tabs toggle panels */
  .app-workspace {
    flex-direction: column;
  }
  .app-resizer { display: none; }

  .app-editors,
  .app-preview-wrap { flex: 1 1 auto !important; display: none; border: none; }

  /* CODE view: show the editor column, hide its console */
  .app-workspace[data-mobile-view="code"] .app-editors { display: flex; }
  .app-workspace[data-mobile-view="code"] .app-console { display: none; }

  /* PREVIEW view: show the preview column full-height */
  .app-workspace[data-mobile-view="preview"] .app-preview-wrap { display: flex; }

  /* CONSOLE view: show the editor column but only the console (expanded) */
  .app-workspace[data-mobile-view="console"] .app-editors { display: flex; }
  .app-workspace[data-mobile-view="console"] .app-editor-tabs,
  .app-workspace[data-mobile-view="console"] .app-editor-stack { display: none; }
  .app-workspace[data-mobile-view="console"] .app-console {
    flex: 1 1 auto; height: auto !important; border-top: none;
  }
  .app-workspace[data-mobile-view="console"] .app-console__log { display: block; }

  /* touch targets */
  .app-icon-btn { width: 40px; height: 40px; }
  .app-btn { height: 38px; }

  /* drawer becomes near-full-screen sheet */
  .app-drawer { width: 100vw; border-left: none; }

  .app-toasts { max-width: calc(100vw - 24px); }

  /* status bar: keep only the essentials on phones */
  #statusLines, #statusLang, .app-status--hide-sm, .app-status--engine { display: none; }
}

/* very small phones */
@media (max-width: 380px) {
  .app-title { font-size: 0.82rem; }
  .app-toolbar { gap: var(--space-1); padding: 0 var(--space-2); }
  .app-header { gap: var(--space-2); padding: 0 var(--space-2); }
}

/* ============================================================
   ULTRAWIDE — keep editor/preview from stretching awkwardly
   ============================================================ */
@media (min-width: 2200px) {
  :root { --editor-font-size: 15px; --toolbar-height: 52px; }
  .app-preview-stage[data-device="responsive"] { justify-content: stretch; }
}

/* ============================================================
   ACCESSIBILITY — reduced motion / high contrast
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}
@media (prefers-contrast: more) {
  :root { --app-border: currentColor; }
}

/* scrollbars */
.app-shell ::-webkit-scrollbar { width: 10px; height: 10px; }
.app-shell ::-webkit-scrollbar-thumb { background: var(--app-border-strong); border-radius: 6px; border: 2px solid transparent; background-clip: padding-box; }
.app-shell ::-webkit-scrollbar-thumb:hover { background: var(--app-text-muted); background-clip: padding-box; }
.app-shell ::-webkit-scrollbar-track { background: transparent; }

[hidden] { display: none !important; }
