/* Application chrome: topbar, sidebar, panes, outline, dialogs. */

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: 100;
  padding: 0.6rem 1rem;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.skip-link:focus { left: 0.5rem; top: 0.5rem; }

.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: var(--topbar-height) 1fr var(--statusbar-height);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* ------------------------------------------------------------------ *
 * Topbar
 * ------------------------------------------------------------------ */

.topbar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0 0.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg-raised);
  padding-top: env(safe-area-inset-top);
}

.topbar-group { display: flex; align-items: center; gap: 0.15rem; }

.topbar-title {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}

.doc-title {
  min-width: 0;
  max-width: 34ch;
  flex: 0 1 auto;
  padding: 0.25rem 0.5rem;
  font-size: 13px;
  font-weight: 600;
  text-align: center;
  text-overflow: ellipsis;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius);
}
.doc-title:hover { border-color: var(--border); }
.doc-title:focus { border-color: var(--accent); background: var(--bg); text-align: left; outline: none; }

.save-state {
  font-size: 11px;
  color: var(--fg-subtle);
  white-space: nowrap;
  min-width: 4ch;
}
.save-state[data-state="dirty"] { color: var(--fg-muted); }
.save-state[data-state="saved"] { color: var(--fg-subtle); }

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border-radius: var(--radius);
  color: var(--fg-muted);
  transition: background 120ms var(--ease), color 120ms var(--ease);
}
.icon-button:hover { background: var(--bg-hover); color: var(--fg); }
.icon-button[aria-pressed="true"] { color: var(--accent); }
.icon-button svg { fill: currentColor; }

.icon-moon { display: none; }
[data-theme="dark"] .icon-sun { display: none; }
[data-theme="dark"] .icon-moon { display: block; }

.view-switch {
  display: flex;
  padding: 2px;
  gap: 2px;
  background: var(--bg-sunken);
  border-radius: var(--radius);
}
.view-switch button {
  padding: 0.22rem 0.6rem;
  font-size: 12px;
  font-weight: 500;
  color: var(--fg-muted);
  border-radius: calc(var(--radius) - 2px);
}
.view-switch button:hover { color: var(--fg); }
.view-switch button[aria-checked="true"] {
  background: var(--bg-raised);
  color: var(--fg);
  box-shadow: var(--shadow-sm);
}

.text-button {
  padding: 0.25rem 0.5rem;
  font-size: 12px;
  color: var(--fg-muted);
  border-radius: 6px;
}
.text-button:hover { background: var(--bg-hover); color: var(--fg); }
.text-button.danger:hover { background: color-mix(in srgb, #e5484d 14%, transparent); color: #e5484d; }

.primary-button {
  padding: 0.5rem 1rem;
  font-size: 13px;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border-radius: var(--radius);
}
.primary-button:hover { filter: brightness(1.08); }

/* ------------------------------------------------------------------ *
 * Workspace
 * ------------------------------------------------------------------ */

.workspace {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  /* Grid and flex items default to a content-based minimum size, which lets a
     wide code block push the whole layout past the viewport. Every container
     between here and the article opts out. */
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.sidebar, .outline {
  position: relative;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--bg-sunken);
  overflow: hidden;
}
.sidebar { width: var(--sidebar-width); border-right: 1px solid var(--border); }
.outline { width: var(--outline-width); border-left: 1px solid var(--border); }

.app[data-library="hidden"] .sidebar,
.app[data-outline="hidden"] .outline { display: none; }

.sidebar-head { padding: 0.5rem; }
.library-filter {
  width: 100%;
  padding: 0.35rem 0.6rem;
  font-size: 12px;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.library-filter:focus { border-color: var(--accent); outline: none; }

.sidebar-actions {
  display: flex;
  gap: 0.25rem;
  padding: 0 0.5rem 0.5rem;
}

.doc-list { flex: 1; overflow-y: auto; padding: 0 0.35rem 0.5rem; }

.doc-row {
  position: relative;
  display: flex;
  align-items: center;
}

.doc-item {
  display: block;
  flex: 1;
  min-width: 0;
  padding: 0.45rem 0.55rem;
  text-align: left;
  border-radius: var(--radius);
  color: inherit;
}
.doc-item:hover { background: var(--bg-hover); }

.doc-delete {
  position: absolute;
  right: 0.3rem;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  color: var(--fg-subtle);
  background: var(--bg-hover);
  border-radius: 6px;
  opacity: 0;
  transition: opacity 120ms var(--ease), color 120ms var(--ease);
}
.doc-delete svg { fill: currentColor; }
.doc-row:hover .doc-delete,
.doc-row:focus-within .doc-delete { opacity: 1; }
.doc-delete:hover { color: #e5484d; background: color-mix(in srgb, #e5484d 16%, transparent); }
.doc-delete:focus-visible { opacity: 1; }

/* With no pointer to hover, the button has to be there all along. */
@media (pointer: coarse) {
  .doc-delete { opacity: 1; background: transparent; }
}
.doc-item.is-current { background: var(--accent-soft); }
[data-theme="dark"] .doc-item.is-current { background: var(--bg-active); }

.doc-item-title {
  display: block;
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.doc-item.is-current .doc-item-title { color: var(--accent); }
.doc-item-meta {
  display: flex;
  gap: 0.4rem;
  font-size: 11px;
  color: var(--fg-subtle);
}
.doc-group-label {
  padding: 0.6rem 0.55rem 0.25rem;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--fg-subtle);
}
.doc-list-empty {
  padding: 1.5rem 0.75rem;
  font-size: 12px;
  color: var(--fg-subtle);
  text-align: center;
}

.sidebar-foot {
  padding: 0.5rem;
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--fg-subtle);
}
.sidebar-foot-actions { display: flex; gap: 0.25rem; margin-top: 0.35rem; }
.storage-line { display: flex; justify-content: space-between; }

.sidebar-resizer, .outline-resizer {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 7px;
  cursor: col-resize;
  z-index: 5;
}
.sidebar-resizer { right: -3px; }
.outline-resizer { left: -3px; }
.sidebar-resizer:hover, .outline-resizer:hover { background: color-mix(in srgb, var(--accent) 35%, transparent); }

/* Outline ---------------------------------------------------------- */

.outline-head {
  padding: 0.7rem 0.85rem 0.4rem;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--fg-subtle);
}
.outline-list { flex: 1; overflow-y: auto; padding: 0 0.5rem 1rem; }
.outline-link {
  display: block;
  padding: 0.22rem 0.5rem;
  font-size: 12.5px;
  line-height: 1.4;
  color: var(--fg-muted);
  text-decoration: none;
  border-left: 2px solid transparent;
  border-radius: 0 4px 4px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.outline-link:hover { color: var(--fg); background: var(--bg-hover); }
.outline-link.is-active { color: var(--accent); border-left-color: var(--accent); font-weight: 500; }
.outline-link[data-level="1"] { font-weight: 600; color: var(--fg); }
.outline-link[data-level="2"] { padding-left: 0.75rem; }
.outline-link[data-level="3"] { padding-left: 1.4rem; }
.outline-link[data-level="4"] { padding-left: 2rem; }
.outline-link[data-level="5"], .outline-link[data-level="6"] { padding-left: 2.6rem; font-size: 12px; }
.outline-empty { padding: 1rem 0.75rem; font-size: 12px; color: var(--fg-subtle); }

/* ------------------------------------------------------------------ *
 * Panes
 * ------------------------------------------------------------------ */

.panes { display: flex; min-width: 0; min-height: 0; }
.pane { min-width: 0; min-height: 0; display: flex; flex-direction: column; }

.pane-source { flex: 0 0 var(--editor-width, 50%); border-right: 1px solid var(--border); }
.pane-preview { flex: 1 1 auto; position: relative; }

.app[data-view="reader"] .pane-source,
.app[data-view="reader"] .pane-splitter { display: none; }
.app[data-view="source"] .pane-preview,
.app[data-view="source"] .pane-splitter { display: none; }
.app[data-view="source"] .pane-source { flex: 1 1 auto; border-right: none; }

.pane-splitter { flex: 0 0 5px; cursor: col-resize; background: transparent; }
.pane-splitter:hover { background: color-mix(in srgb, var(--accent) 35%, transparent); }

.editor {
  flex: 1;
  width: 100%;
  padding: 1.5rem 1.75rem 6rem;
  font-family: var(--mono-font);
  font-size: 13.5px;
  line-height: 1.65;
  tab-size: 2;
  color: var(--fg);
  background: var(--bg);
  border: none;
  resize: none;
  outline: none;
  overflow-y: auto;
}
.editor::placeholder { color: var(--fg-subtle); }

.reading-progress {
  position: absolute;
  top: 0;
  left: 0;
  height: 2px;
  width: 0;
  background: var(--accent);
  z-index: 4;
  transition: width 90ms linear;
}

.scroll-region {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  overflow-x: hidden;
  scroll-behavior: smooth;
  outline: none;
  scroll-padding-top: 1.5rem;
}

.doc-meta {
  max-width: var(--reader-measure);
  margin: 2rem auto 0;
  padding: 0.75rem 1rem;
  font-size: 12px;
  color: var(--fg-muted);
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.doc-meta dl { display: grid; grid-template-columns: auto 1fr; gap: 0.15rem 0.75rem; margin: 0; }
.doc-meta dt { font-weight: 600; color: var(--fg-subtle); }
.doc-meta dd { margin: 0; }

/* Empty state ------------------------------------------------------ */

.empty-state {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 0.35rem;
  padding: 5rem 1.5rem;
  text-align: center;
  color: var(--fg-muted);
}
.app[data-empty="true"] .empty-state { display: flex; }
.app[data-empty="true"] .markdown-body,
.app[data-empty="true"] .doc-meta { display: none; }
.empty-state svg { color: var(--border-strong); margin-bottom: 0.5rem; }
.empty-state h2 { font-size: 16px; color: var(--fg); }
.empty-state p { font-size: 13px; max-width: 38ch; }
.empty-actions { display: flex; gap: 0.5rem; margin-top: 0.75rem; align-items: center; }
.empty-note { font-size: 12px; color: var(--fg-subtle); margin-top: 1.5rem; }

/* ------------------------------------------------------------------ *
 * Status bar
 * ------------------------------------------------------------------ */

.statusbar {
  display: flex;
  align-items: center;
  gap: 0.85rem;
  padding: 0 0.85rem;
  font-size: 11px;
  color: var(--fg-subtle);
  background: var(--bg-raised);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.statusbar .status-item { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status-spacer { flex: 1; }
.status-offline {
  padding: 0.05rem 0.4rem;
  border-radius: 999px;
  background: var(--bg-sunken);
  border: 1px solid var(--border);
}

/* ------------------------------------------------------------------ *
 * Command palette
 * ------------------------------------------------------------------ */

.palette {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: start center;
  padding-top: 12vh;
}
.palette[hidden] { display: none; }
.palette-backdrop { position: absolute; inset: 0; background: var(--overlay); backdrop-filter: blur(2px); }

.palette-panel {
  position: relative;
  width: min(620px, calc(100vw - 2rem));
  max-height: 70vh;
  display: flex;
  flex-direction: column;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  animation: palette-in 140ms var(--ease);
}
@keyframes palette-in { from { opacity: 0; transform: translateY(-6px) scale(0.99); } }

.palette-search { padding: 0.4rem; border-bottom: 1px solid var(--border); }
.palette-input {
  width: 100%;
  padding: 0.55rem 0.7rem;
  font-size: 14px;
  background: transparent;
  border: none;
  outline: none;
}
.palette-list { flex: 1; overflow-y: auto; padding: 0.35rem; list-style: none; margin: 0; }
.palette-item {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 0.5rem;
  padding: 0.45rem 0.6rem;
  border-radius: var(--radius);
  cursor: pointer;
}
.palette-item.is-active { background: var(--accent-soft); }
[data-theme="dark"] .palette-item.is-active { background: var(--bg-active); }
.palette-item-title { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.palette-item-detail { font-size: 11px; color: var(--fg-subtle); justify-self: end; }
.palette-item-key { font-size: 10px; color: var(--fg-subtle); }
.palette-item-snippet {
  grid-column: 1 / -1;
  font-size: 11.5px;
  color: var(--fg-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.palette-item-snippet mark { background: color-mix(in srgb, var(--accent) 28%, transparent); color: inherit; border-radius: 2px; }
.palette-empty, .palette-hint { padding: 0.75rem; font-size: 12px; color: var(--fg-subtle); text-align: center; }
.palette-hint { border-top: 1px solid var(--border); padding: 0.4rem; }

/* ------------------------------------------------------------------ *
 * Settings sheet
 * ------------------------------------------------------------------ */

.sheet {
  width: min(560px, calc(100vw - 2rem));
  max-height: 84vh;
  padding: 0;
  color: var(--fg);
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
}
.sheet::backdrop { background: var(--overlay); backdrop-filter: blur(2px); }

.sheet-head {
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.85rem 1rem;
  background: var(--bg-raised);
  border-bottom: 1px solid var(--border);
  z-index: 1;
}
.sheet-head h2 { font-size: 14px; }
.sheet-body { padding: 0.5rem 1rem 1.5rem; overflow-y: auto; max-height: calc(84vh - 52px); }

.sheet-narrow { width: min(520px, calc(100vw - 2rem)); }

.sheet-confirm { width: min(400px, calc(100vw - 2rem)); }
.confirm-body { padding: 1.25rem 1.25rem 0.5rem; }
.confirm-body h2 { font-size: 15px; margin-bottom: 0.4rem; }
.confirm-body p { font-size: 13px; line-height: 1.5; color: var(--fg-muted); overflow-wrap: break-word; }
.confirm-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.4rem;
  padding: 0.85rem 1.25rem 1.25rem;
}
.danger-button { background: #e5484d; }
.danger-button:hover { background: #d13438; filter: none; }

.paste-input {
  width: 100%;
  min-height: 40vh;
  margin: 0.5rem 0;
  padding: 0.75rem 0.85rem;
  font-family: var(--mono-font);
  font-size: 13px;
  line-height: 1.6;
  tab-size: 2;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  resize: vertical;
}
.paste-input:focus { border-color: var(--accent); outline: none; }
.paste-input::placeholder { color: var(--fg-subtle); }

.paste-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
}
.paste-count { font-size: 12px; color: var(--fg-subtle); font-variant-numeric: tabular-nums; }
.paste-actions { display: flex; align-items: center; gap: 0.4rem; margin-left: auto; }

.setting-group { padding: 1rem 0; border-bottom: 1px solid var(--border); }
.setting-group:last-child { border-bottom: none; }
.setting-group h3 {
  margin-bottom: 0.6rem;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: var(--fg-subtle);
}
.setting {
  display: grid;
  grid-template-columns: 1fr 160px;
  align-items: center;
  gap: 0.75rem;
  padding: 0.3rem 0;
  font-size: 13px;
}
.setting select, .setting input[type="range"] { width: 100%; }
.setting select {
  padding: 0.25rem 0.4rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
}
.setting-switch { grid-template-columns: 20px 1fr; }
.setting-switch input { justify-self: start; accent-color: var(--accent); }
.setting output { color: var(--fg-subtle); font-variant-numeric: tabular-nums; }
.setting-note { font-size: 12px; color: var(--fg-subtle); margin: 0.35rem 0; }
.setting-buttons { display: flex; flex-wrap: wrap; gap: 0.35rem; margin: 0.5rem 0; }

.shortcut-list { display: grid; gap: 0.3rem; font-size: 12.5px; }
.shortcut-list li { display: flex; align-items: center; gap: 0.3rem; }
.shortcut-list span { margin-left: auto; color: var(--fg-muted); }

kbd {
  display: inline-block;
  min-width: 1.4em;
  padding: 0.1em 0.35em;
  font-family: var(--ui-font);
  font-size: 0.85em;
  text-align: center;
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
}

/* ------------------------------------------------------------------ *
 * Overlays
 * ------------------------------------------------------------------ */

.drop-overlay {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: grid;
  place-items: center;
  background: var(--overlay);
  backdrop-filter: blur(2px);
}
.drop-overlay[hidden] { display: none; }
.drop-card {
  display: grid;
  gap: 0.25rem;
  justify-items: center;
  padding: 2rem 3rem;
  text-align: center;
  background: var(--bg-raised);
  border: 2px dashed var(--accent);
  border-radius: var(--radius-lg);
}
.drop-card strong { font-size: 15px; }
.drop-card span { font-size: 12px; color: var(--fg-muted); }

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: grid;
  place-items: center;
  padding: 3rem 1.5rem;
  background: var(--overlay);
  backdrop-filter: blur(3px);
}
.lightbox[hidden] { display: none; }
.lightbox-body {
  max-width: 96vw;
  max-height: 88vh;
  overflow: auto;
  padding: 1.25rem;
  background: var(--bg-raised);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
}
.lightbox-body svg, .lightbox-body img { max-width: none; width: auto; height: auto; }
.lightbox-close {
  position: absolute;
  top: 1rem;
  right: 1rem;
  width: 34px;
  height: 34px;
  font-size: 14px;
  color: var(--fg);
  background: var(--bg-raised);
  border-radius: 999px;
  box-shadow: var(--shadow-sm);
}

.toasts {
  position: fixed;
  bottom: calc(var(--statusbar-height) + 1rem);
  left: 50%;
  transform: translateX(-50%);
  z-index: 90;
  display: grid;
  gap: 0.4rem;
  justify-items: center;
  /* A long document title used to stretch the toast past both screen edges. */
  max-width: min(560px, calc(100vw - 2rem));
  pointer-events: none;
}
.toast {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  max-width: 100%;
  padding: 0.5rem 0.9rem;
  font-size: 12.5px;
  color: var(--fg);
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 999px;
  box-shadow: var(--shadow-lg);
  animation: toast-in 160ms var(--ease);
  pointer-events: auto;
}
.toast.is-leaving { animation: toast-out 160ms var(--ease) forwards; }
.toast-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.toast button { flex: none; font-size: 12px; font-weight: 600; color: var(--accent); }
.toast-close { color: var(--fg-subtle); font-weight: 400; }
.toast-close:hover { color: var(--fg); }
.toast-error { border-color: color-mix(in srgb, #e5484d 45%, var(--border)); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }
@keyframes toast-out { to { opacity: 0; transform: translateY(6px); } }

.noscript {
  max-width: 40rem;
  margin: 4rem auto;
  padding: 0 1.5rem;
  text-align: center;
}
.noscript h1 { font-size: 1.4rem; margin-bottom: 0.75rem; }

/* ------------------------------------------------------------------ *
 * Narrow screens
 * ------------------------------------------------------------------ */

@media (max-width: 860px) {
  .app { grid-template-rows: var(--topbar-height) 1fr auto; }
  .workspace { grid-template-columns: minmax(0, 1fr); position: relative; }

  /* The panels overlay the document rather than sitting beside it, so only
     one is ever open and a scrim makes it obvious the document is behind. */
  .sidebar, .outline {
    position: absolute;
    top: 0;
    bottom: 0;
    z-index: 20;
    width: min(320px, 84vw);
    box-shadow: var(--shadow-lg);
  }
  .sidebar { left: 0; }
  .outline { right: 0; }
  .sidebar-resizer, .outline-resizer { display: none; }

  .workspace::after {
    content: '';
    position: absolute;
    inset: 0;
    z-index: 15;
    background: var(--overlay);
    opacity: 0;
    pointer-events: none;
    transition: opacity 140ms var(--ease);
  }
  .app[data-panel-open="true"] .workspace::after { opacity: 1; pointer-events: auto; }

  .app[data-view="split"] .pane-source { flex-basis: 50%; }
  .doc-title { max-width: 18ch; }
  .view-switch button { padding: 0.22rem 0.45rem; }
  .statusbar { font-size: 10px; gap: 0.5rem; }
}

@media (max-width: 560px) {
  .app[data-view="split"] .panes { flex-direction: column; }
  .app[data-view="split"] .pane-source { flex: 1 1 50%; border-right: none; border-bottom: 1px solid var(--border); }
  .pane-splitter { display: none; }
  .topbar-title { order: 3; flex-basis: 100%; }
}
