:root {
  color-scheme: dark;
  --bg: #0d0f12;
  --panel: #15181d;
  --panel-2: #1d2128;
  --line: #2c323b;
  --text: #f4f6f8;
  --muted: #9ba5b2;
  --accent: #d9ff62;
  --accent-text: #10130b;
  --safe-top: env(safe-area-inset-top);
  --safe-bottom: env(safe-area-inset-bottom);
  --focus-bar-h: 44px;
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
}
button, textarea, input { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }

.app-shell {
  width: min(100%, 1000px);
  min-height: 100dvh;
  margin: 0 auto;
  padding: calc(18px + var(--safe-top)) 16px calc(18px + var(--safe-bottom));
  display: grid;
  grid-template-rows: auto auto auto auto minmax(0, 1fr) auto;
  gap: 12px;
}

.topbar {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 16px;
}
.topbar-actions {
  display: flex;
  align-items: end;
  gap: 8px;
}
.eyebrow {
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .12em;
}
h1 {
  margin: 3px 0 0;
  font-size: clamp(28px, 7vw, 40px);
  letter-spacing: -.04em;
}
.status {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
  padding-bottom: 5px;
}
.focus-button {
  min-height: 32px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--panel);
  color: var(--text);
  font-size: 11px;
  font-weight: 750;
  white-space: nowrap;
}
.focus-button:active { transform: scale(.97); }

.pocket-switch {
  display: grid;
  grid-auto-columns: 1fr;
  grid-auto-flow: column;
  padding: 3px;
  background: #111318;
  border: 1px solid var(--line);
  border-radius: 12px;
}
.pocket-switch-link {
  min-height: 34px;
  border-radius: 9px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: .05em;
  text-decoration: none;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}
.pocket-switch-link.active {
  background: var(--panel-2);
  color: var(--accent);
}

.mode-switch {
  display: grid;
  grid-template-columns: 1fr 1fr;
  padding: 3px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 12px;
}
.mode-button {
  min-height: 40px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  font-weight: 650;
}
.mode-button.active {
  background: #f5f5f5;
  color: #111;
}

.toolbar {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
}
.toolbar::-webkit-scrollbar { display: none; }
.button {
  min-height: 42px;
  padding: 0 15px;
  border: 1px solid var(--line);
  border-radius: 11px;
  font-weight: 700;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.button.secondary { background: var(--panel); color: var(--text); }
.button.accent { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.file-button { position: relative; overflow: hidden; cursor: pointer; }
.file-button input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

.pane {
  min-height: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
}
.code-pane {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
}
.filename-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 42px;
  padding: 0 12px;
  border-bottom: 1px solid var(--line);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
  font-size: 12px;
}
.hint { color: var(--muted); font-family: inherit; }
#editor {
  width: 100%;
  height: 100%;
  min-height: 48dvh;
  resize: none;
  border: 0;
  outline: 0;
  padding: 15px;
  background: #101216;
  color: #f3f5f7;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 14px;
  line-height: 1.55;
  tab-size: 2;
  caret-color: var(--accent);
  white-space: pre;
  overflow: auto;
  -webkit-text-size-adjust: 100%;
}
.symbol-bar {
  display: flex;
  gap: 5px;
  padding: 8px;
  overflow-x: auto;
  border-top: 1px solid var(--line);
  background: var(--panel-2);
  scrollbar-width: none;
}
.symbol-bar::-webkit-scrollbar { display: none; }
.symbol-bar button {
  min-width: 42px;
  height: 38px;
  border: 1px solid #3a424d;
  border-radius: 8px;
  background: #242931;
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 16px;
}

.preview-pane { background: white; }
#preview {
  width: 100%;
  height: 100%;
  min-height: 62dvh;
  border: 0;
  background: white;
  touch-action: pan-y pinch-zoom;
}
[hidden] { display: none !important; }
footer { color: var(--muted); font-size: 11px; text-align: center; }

.focus-bar {
  position: fixed;
  z-index: 1000;
  inset: 0 0 auto 0;
  height: calc(var(--safe-top) + var(--focus-bar-h));
  padding: var(--safe-top) 10px 0;
  background: rgba(13,15,18,.97);
  border-bottom: 1px solid var(--line);
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.focus-bar:not([hidden]) { display: flex; }
.focus-bar-label {
  color: var(--muted);
  font-size: 11px;
  font-weight: 750;
  letter-spacing: .04em;
  white-space: nowrap;
}
.restore-controls {
  min-height: 32px;
  padding: 0 12px;
  border: 1px solid rgba(255,255,255,.18);
  border-radius: 999px;
  background: var(--panel-2);
  color: #fff;
  font-size: 11px;
  font-weight: 750;
  letter-spacing: .01em;
}
.restore-controls:active { transform: scale(.97); }

body.focus-preview {
  height: 100dvh;
  overflow: hidden;
}
body.focus-preview .app-shell {
  width: 100%;
  height: 100dvh;
  min-height: 100dvh;
  padding: 0;
  gap: 0;
  display: block;
}
body.focus-preview .topbar,
body.focus-preview .pocket-switch,
body.focus-preview .mode-switch,
body.focus-preview .toolbar,
body.focus-preview .code-pane,
body.focus-preview footer {
  display: none !important;
}
body.focus-preview .preview-pane {
  display: block !important;
  position: fixed;
  z-index: 1;
  top: calc(var(--safe-top) + var(--focus-bar-h));
  right: 0;
  bottom: 0;
  left: 0;
  width: auto;
  height: auto;
  min-height: 0;
  overflow: hidden;
  border: 0;
  border-radius: 0;
  background: white;
  touch-action: pan-y pinch-zoom;
}
body.focus-preview #preview {
  display: block;
  width: 100%;
  height: 100%;
  min-height: 0;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y pinch-zoom;
}

@media (max-width: 520px) {
  .topbar { gap: 10px; }
  .topbar-actions {
    flex-direction: column;
    align-items: flex-end;
    gap: 4px;
  }
  .status { padding-bottom: 0; }
  .focus-button { min-height: 30px; padding: 0 9px; }
}

@media (min-width: 780px) {
  .app-shell { padding-left: 24px; padding-right: 24px; }
  #editor, #preview { min-height: 66dvh; }
  body.focus-preview #preview { min-height: 0; }
}

/* ---- Library sheet ---- */
body.sheet-open { overflow: hidden; }
.sheet {
  position: fixed;
  z-index: 2000;
  inset: 0;
  background: rgba(0,0,0,.55);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.sheet[hidden] { display: none !important; }
.sheet-panel {
  width: min(100%, 640px);
  max-height: 86dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 12px;
  padding: 16px 16px calc(16px + var(--safe-bottom));
  background: var(--panel);
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: 18px 18px 0 0;
}
.sheet-head {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 12px;
}
.sheet-head h2 { margin: 3px 0 0; font-size: 24px; letter-spacing: -.03em; }
.library-list { overflow-y: auto; display: grid; gap: 8px; align-content: start; }
.library-empty { color: var(--muted); font-size: 14px; margin: 8px 0; }
.library-item {
  display: flex;
  gap: 8px;
  align-items: stretch;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel-2);
  overflow: hidden;
}
.library-item.current { border-color: var(--accent); }
.library-open {
  flex: 1;
  min-width: 0;
  display: grid;
  gap: 3px;
  padding: 12px;
  border: 0;
  background: transparent;
  color: var(--text);
  text-align: left;
}
.library-name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.library-meta { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.library-delete {
  padding: 0 14px;
  border: 0;
  border-left: 1px solid var(--line);
  background: transparent;
  color: #ff8f80;
  font-size: 13px;
  font-weight: 700;
}
.library-tools-link {
  color: var(--accent);
  font-size: 14px;
  font-weight: 700;
  text-decoration: none;
  text-align: center;
  padding: 6px;
}
