:root {
  --accent: #4f46e5;
  --accent-2: #2563eb;
  --accent-contrast: #ffffff;
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --surface-hover: #e2e8f0;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --danger: #dc2626;
  --danger-bg: #fef2f2;
  --ok: #16a34a;
  --shadow: 0 8px 24px rgba(15, 23, 42, 0.12), 0 2px 6px rgba(15, 23, 42, 0.08);
  --radius: 10px;
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", "Malgun Gothic", "Apple SD Gothic Neo", Roboto, sans-serif;
  --font-mono: ui-monospace, "Cascadia Code", "D2Coding", "Consolas", "Nanum Gothic Coding", SFMono-Regular, Menlo, monospace;
  --editor-font-size: 14px;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --accent: #818cf8;
  --accent-2: #60a5fa;
  --accent-contrast: #0b1120;
  --bg: #0f172a;
  --surface: #16213a;
  --surface-2: #111a2e;
  --surface-hover: #1e293b;
  --border: #263449;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --danger: #f87171;
  --danger-bg: #3f1d1d;
  --ok: #4ade80;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.45), 0 2px 6px rgba(0, 0, 0, 0.3);
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --accent: #818cf8;
    --accent-2: #60a5fa;
    --accent-contrast: #0b1120;
    --bg: #0f172a;
    --surface: #16213a;
    --surface-2: #111a2e;
    --surface-hover: #1e293b;
    --border: #263449;
    --text: #e2e8f0;
    --text-muted: #94a3b8;
    --danger: #f87171;
    --danger-bg: #3f1d1d;
    --ok: #4ade80;
    --shadow: 0 8px 24px rgba(0, 0, 0, 0.45), 0 2px 6px rgba(0, 0, 0, 0.3);
    color-scheme: dark;
  }
}

[hidden] { display: none !important; }
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-ui);
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button { font-family: inherit; }
svg { display: block; }

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

/* ---------- Toolbar ---------- */
.toolbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
  overflow-x: auto;
  scrollbar-width: thin;
}
.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 10px 4px 4px;
  margin-right: 4px;
  flex: 0 0 auto;
}
.brand img { width: 24px; height: 24px; border-radius: 6px; }
.brand span { font-weight: 700; font-size: 14px; white-space: nowrap; }
.toolbar .sep {
  width: 1px;
  align-self: stretch;
  margin: 6px 4px;
  background: var(--border);
  flex: 0 0 auto;
}
.toolbar .spacer { flex: 1 1 auto; min-width: 8px; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 32px;
  padding: 0 10px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  white-space: nowrap;
  flex: 0 0 auto;
  text-decoration: none;
}
.icon-btn svg { width: 16px; height: 16px; flex: 0 0 auto; }
.icon-btn:hover { background: var(--surface-hover); }
.icon-btn:active { transform: translateY(1px); }
.icon-btn.primary { background: var(--accent); color: var(--accent-contrast); }
.icon-btn.primary:hover { filter: brightness(1.08); }
.icon-btn.icon-only { padding: 0; width: 32px; justify-content: center; }
.icon-btn[disabled] { opacity: 0.4; cursor: not-allowed; pointer-events: none; }
.icon-btn .label-sm { font-size: 13px; }

@media (max-width: 720px) {
  .icon-btn .btn-text { display: none; }
  .icon-btn { width: 32px; padding: 0; justify-content: center; }
  .icon-btn.primary { width: auto; padding: 0 10px; }
  .icon-btn.primary .btn-text { display: inline; }
}

/* ---------- Tab bar ---------- */
.tabbar {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  padding: 6px 8px 0;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  scrollbar-width: thin;
  flex: 0 0 auto;
}
.tabbar:empty { display: none; }
.tab {
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: 220px;
  min-width: 120px;
  height: 34px;
  padding: 0 6px 0 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: 8px 8px 0 0;
  color: var(--text-muted);
  cursor: pointer;
  position: relative;
  top: 1px;
  flex: 0 0 auto;
}
.tab:hover { color: var(--text); }
.tab.active {
  color: var(--text);
  background: var(--bg);
  border-color: var(--border);
  box-shadow: inset 0 2px 0 var(--accent);
}
.tab .tab-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  flex: 1 1 auto;
}
.tab .tab-name-input {
  font: inherit;
  font-size: 13px;
  width: 100%;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--accent);
  border-radius: 4px;
  padding: 1px 4px;
}
.tab .dirty-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  flex: 0 0 auto;
  display: none;
}
.tab.dirty .dirty-dot { display: block; }
.tab.dirty .tab-close { display: none; }
.tab.dirty:hover .dirty-dot { display: none; }
.tab.dirty:hover .tab-close { display: flex; }
.tab-close {
  width: 18px;
  height: 18px;
  border-radius: 5px;
  border: none;
  background: transparent;
  color: inherit;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  cursor: pointer;
}
.tab-close:hover { background: var(--surface-hover); color: var(--danger); }
.tab-close svg { width: 12px; height: 12px; }

/* ---------- Workspace ---------- */
.workspace {
  flex: 1 1 auto;
  display: flex;
  min-height: 0;
  background: var(--bg);
}
#editor-pane {
  flex: 1 1 50%;
  min-width: 0;
  position: relative;
}
#editor {
  position: absolute;
  inset: 0;
}
.CodeMirror {
  height: 100%;
  font-family: var(--font-mono);
  font-size: var(--editor-font-size);
  line-height: 1.55;
}
#preview-pane {
  flex: 1 1 50%;
  min-width: 0;
  border-left: 1px solid var(--border);
  display: none;
  background: #fff;
}
#preview-pane.visible { display: block; }
#preview-frame { width: 100%; height: 100%; border: 0; background: #fff; }

.pdf-preview {
  width: 100%;
  height: 100%;
  overflow-y: auto;
  background: #525659;
  box-sizing: border-box;
  padding: 20px 0 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}
.pdf-preview canvas.pdf-page {
  background: #fff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
  max-width: calc(100% - 32px);
  height: auto;
}
.pdf-preview .pdf-preview-loading,
.pdf-preview .pdf-preview-note {
  color: #e2e8f0;
  font-size: 13px;
  padding: 10px 16px;
  text-align: center;
}

.empty-state {
  position: absolute;
  inset: 0;
  background: var(--bg);
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  color: var(--text-muted);
  text-align: center;
  padding: 24px;
}
.empty-state.visible { display: flex; }
.empty-state svg { width: 56px; height: 56px; opacity: 0.5; }
.empty-state h2 { margin: 0; font-size: 16px; color: var(--text); font-weight: 600; }
.empty-state p { margin: 0; font-size: 13px; max-width: 320px; }
.empty-state .icon-btn { margin-top: 6px; }
.empty-state a {
  color: var(--text-muted);
  font-size: 12px;
  text-decoration: none;
}
.empty-state a:hover { color: var(--accent); text-decoration: underline; }

/* ---------- Install banner ---------- */
.install-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: var(--accent);
  color: var(--accent-contrast);
  flex: 0 0 auto;
}
.install-banner-text { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.install-banner-text strong { font-size: 13px; }
.install-banner-text span { font-size: 12px; opacity: 0.9; }
.install-banner-actions { display: flex; align-items: center; gap: 4px; flex: 0 0 auto; }
.install-banner .icon-btn.primary { background: var(--accent-contrast); color: var(--accent); }
.install-banner .icon-btn.icon-only { color: var(--accent-contrast); }
.install-banner .icon-btn.icon-only:hover { background: rgba(255, 255, 255, 0.2); }
@media (max-width: 480px) {
  .install-banner-text span { display: none; }
}

/* ---------- Ad slots (AdSense placeholders) ---------- */
.ad-slot {
  width: 100%;
  max-width: 336px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px dashed var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text-muted);
  overflow: hidden;
}
.ad-slot-label {
  font-size: 11px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}
.ad-slot--rectangle { min-height: 100px; }
.ad-slot--leaderboard { max-width: 728px; min-height: 90px; }
.ad-slot--banner { max-width: 100%; min-height: 50px; }

/* ---------- Drop zone ---------- */
#dropzone-overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(79, 70, 229, 0.12);
  backdrop-filter: blur(2px);
  display: none;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
#dropzone-overlay.visible { display: flex; }
.dropzone-box {
  border: 3px dashed var(--accent);
  border-radius: 20px;
  padding: 48px 64px;
  background: var(--surface);
  color: var(--accent);
  font-size: 18px;
  font-weight: 600;
  box-shadow: var(--shadow);
}

/* ---------- Status bar ---------- */
.statusbar {
  display: flex;
  align-items: center;
  height: 26px;
  padding: 0 10px;
  gap: 2px;
  background: var(--accent);
  color: var(--accent-contrast);
  font-size: 12px;
  flex: 0 0 auto;
  overflow-x: auto;
  white-space: nowrap;
}
.status-item {
  display: flex;
  align-items: center;
  height: 100%;
  padding: 0 8px;
  border: none;
  background: transparent;
  color: inherit;
  cursor: pointer;
  font: inherit;
  opacity: 0.92;
  flex: 0 0 auto;
}
.status-item:hover { opacity: 1; background: rgba(255, 255, 255, 0.15); }
.status-item.static { cursor: default; }
.status-item.static:hover { background: transparent; }
.statusbar .spacer { flex: 1 1 auto; }

@media (max-width: 640px) {
  .status-item[data-hide-narrow] { display: none; }
}

/* ---------- Popovers / menus ---------- */
.popover {
  position: fixed;
  z-index: 50;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  color: var(--text);
  display: none;
  min-width: 240px;
  max-width: min(90vw, 340px);
  overflow: hidden;
}
.popover.visible { display: block; }
.popover-header {
  padding: 10px 14px;
  font-weight: 600;
  font-size: 13px;
  border-bottom: 1px solid var(--border);
}
.popover-body { padding: 10px 14px 14px; display: flex; flex-direction: column; gap: 12px; max-height: 60vh; overflow-y: auto; }
.field-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; font-size: 13px; }
.field-row label { color: var(--text-muted); }
.field-row input[type="number"] {
  width: 64px; padding: 4px 6px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--bg); color: var(--text); font: inherit;
}
.field-row select {
  padding: 4px 6px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--bg); color: var(--text); font: inherit; max-width: 160px;
}
.seg {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 7px;
  overflow: hidden;
}
.seg button {
  border: none;
  background: var(--surface);
  color: var(--text-muted);
  font: inherit;
  font-size: 12px;
  padding: 5px 10px;
  cursor: pointer;
}
.seg button + button { border-left: 1px solid var(--border); }
.seg button.active { background: var(--accent); color: var(--accent-contrast); }
.toggle-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; font-size: 13px; }
.switch {
  position: relative; width: 36px; height: 20px; flex: 0 0 auto;
}
.switch input { opacity: 0; width: 0; height: 0; }
.switch .track {
  position: absolute; inset: 0; background: var(--border); border-radius: 999px; transition: 0.15s; cursor: pointer;
}
.switch .track::before {
  content: ""; position: absolute; width: 16px; height: 16px; left: 2px; top: 2px;
  background: #fff; border-radius: 50%; transition: 0.15s; box-shadow: 0 1px 2px rgba(0,0,0,0.3);
}
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track::before { transform: translateX(16px); }

.menu-list { display: flex; flex-direction: column; gap: 2px; padding: 6px; max-height: 320px; overflow-y: auto; }
.menu-list input[type="search"] {
  position: sticky; top: 0; z-index: 1;
  margin: 0 0 6px; padding: 6px 8px; border: 1px solid var(--border); border-radius: 7px;
  background: var(--surface); color: var(--text); font: inherit; font-size: 13px;
}
.menu-item {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 7px 10px; border-radius: 6px; cursor: pointer; font-size: 13px; border: none; background: transparent; color: var(--text); text-align: left;
}
.menu-item:hover { background: var(--surface-hover); }
.menu-item.active { background: var(--accent); color: var(--accent-contrast); }
.menu-item .hint { font-size: 11px; color: var(--text-muted); }
.menu-item.active .hint { color: inherit; opacity: 0.8; }

/* ---------- Modal ---------- */
#modal-backdrop {
  position: fixed; inset: 0; z-index: 60; background: rgba(15, 23, 42, 0.45);
  display: none; align-items: center; justify-content: center; padding: 20px;
}
#modal-backdrop.visible { display: flex; }
.modal {
  background: var(--surface); color: var(--text); border-radius: var(--radius);
  box-shadow: var(--shadow); width: 100%; max-width: 380px; overflow: hidden;
}
.modal-body { padding: 18px 18px 6px; font-size: 14px; line-height: 1.5; }
.modal-body h3 { margin: 0 0 8px; font-size: 15px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 18px 18px; }
.modal-actions .icon-btn { height: 34px; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { filter: brightness(1.08); }

/* ---------- Toasts ---------- */
#toast-container {
  position: fixed; left: 50%; bottom: 40px; transform: translateX(-50%);
  z-index: 70; display: flex; flex-direction: column; gap: 8px; align-items: center;
  pointer-events: none;
}
.toast {
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
  box-shadow: var(--shadow); border-radius: 999px; padding: 8px 16px; font-size: 13px;
  display: flex; align-items: center; gap: 10px; pointer-events: auto;
  animation: toast-in 0.18s ease-out;
}
.toast button {
  border: none; background: transparent; color: var(--accent); font: inherit; font-weight: 600; cursor: pointer; font-size: 13px;
}
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---------- Scrollbars (webkit) ---------- */
.tabbar::-webkit-scrollbar, .toolbar::-webkit-scrollbar, .statusbar::-webkit-scrollbar,
.menu-list::-webkit-scrollbar, .popover-body::-webkit-scrollbar {
  height: 6px; width: 6px;
}
.tabbar::-webkit-scrollbar-thumb, .toolbar::-webkit-scrollbar-thumb,
.menu-list::-webkit-scrollbar-thumb, .popover-body::-webkit-scrollbar-thumb {
  background: var(--border); border-radius: 999px;
}

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* CodeMirror dialog (search/replace) theming */
.CodeMirror-dialog {
  background: var(--surface) !important;
  color: var(--text) !important;
  border-bottom: 1px solid var(--border) !important;
  padding: 6px 10px !important;
}
.CodeMirror-dialog input {
  background: var(--bg) !important;
  color: var(--text) !important;
  border: 1px solid var(--border) !important;
  border-radius: 5px !important;
  padding: 3px 6px !important;
}
