/* =================================================================
   cloud notes — modern UI
   Layout uses an explicit flex chain so every container has a
   definite height. Viewer panes use absolute fill — never flex+100%.
   ================================================================= */

:root {
  /* surfaces */
  --bg-canvas:    #f6f5f1;
  --bg-card:      #ffffff;
  --bg-topbar:    rgba(255, 255, 255, 0.85);
  --bg-sidebar:   #0f0f11;
  --bg-sidebar-2: #18181b;

  /* text */
  --fg:           #18181b;
  --fg-2:         #3f3f46;
  --fg-muted:     #71717a;
  --fg-on-dark:   #f4f4f5;
  --fg-on-dark-2: #a1a1aa;
  --fg-on-dark-3: #52525b;

  /* lines */
  --line:         #ebe9e3;
  --line-strong:  #d8d4cc;
  --line-dark:    #27272a;

  /* accent */
  --accent:       #6366f1;
  --accent-hover: #4f46e5;
  --accent-soft:  #eef2ff;
  --accent-dark:  #c7d2fe;

  /* states */
  --danger:       #ef4444;
  --warning:      #f59e0b;
  --success:      #10b981;

  /* dot grid */
  --dot:          #d8d4cc;

  /* elevations */
  --shadow-xs:    0 1px 1px rgba(24, 24, 27, .04);
  --shadow-sm:    0 1px 2px rgba(24, 24, 27, .05), 0 1px 1px rgba(24, 24, 27, .04);
  --shadow:       0 1px 2px rgba(24, 24, 27, .04), 0 4px 14px rgba(24, 24, 27, .07);
  --shadow-md:    0 4px 8px -2px rgba(24, 24, 27, .08), 0 12px 24px -8px rgba(24, 24, 27, .12);
  --shadow-lg:    0 12px 24px -8px rgba(24, 24, 27, .10), 0 24px 48px -12px rgba(24, 24, 27, .16);

  /* type */
  --font-ui:   -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI Variable", "Segoe UI", system-ui, Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-note: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI Variable", "Segoe UI", system-ui, Roboto, sans-serif;
  --font-mono: "JetBrains Mono", "SF Mono", ui-monospace, Menlo, Consolas, "Liberation Mono", monospace;

  /* timing */
  --t-fast:   120ms;
  --t-base:   180ms;
  --t-slow:   280ms;
  --ease:     cubic-bezier(.22, .61, .36, 1);

  /* radii */
  --r-xs: 4px;
  --r-sm: 6px;
  --r:    8px;
  --r-lg: 12px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font-ui);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: var(--fg);
  background: var(--bg-canvas);
  display: flex;
  overflow: hidden;
}

/* ============================== Sidebar ============================== */
#sidebar {
  width: 248px;
  flex-shrink: 0;
  background: var(--bg-sidebar);
  color: var(--fg-on-dark);
  display: flex;
  flex-direction: column;
  border-right: 1px solid #000;
  user-select: none;
}

#sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 12px;
  font-size: 12px;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--fg-on-dark-2);
}

.brand {
  color: #fff;
  font-weight: 600;
  font-size: 13px;
  letter-spacing: .02em;
  text-transform: none;
  display: flex;
  align-items: center;
  gap: 8px;
}
.brand::before {
  content: "";
  width: 8px; height: 8px;
  background: var(--accent);
  border-radius: 50%;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, .18);
}

.icon-btn {
  background: transparent;
  color: var(--fg-on-dark-2);
  border: 0;
  border-radius: var(--r-xs);
  width: 26px; height: 26px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 14px;
  transition: background var(--t-fast) var(--ease), color var(--t-fast) var(--ease);
}
.icon-btn:hover { background: rgba(255,255,255,.06); color: #fff; }
.icon-btn:active { background: rgba(255,255,255,.10); }

#tree {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 4px 0 16px;
  font-size: 13px;
  outline: none;
}
#tree::-webkit-scrollbar { width: 8px; }
#tree::-webkit-scrollbar-thumb { background: rgba(255,255,255,.08); border-radius: 4px; }
#tree::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,.14); }
#tree::-webkit-scrollbar-track { background: transparent; }

.tree-node { display: block; }
.tree-row {
  position: relative;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 12px 4px 6px;
  margin: 0 6px;
  cursor: pointer;
  white-space: nowrap;
  border-radius: var(--r-xs);
  color: var(--fg-on-dark);
  transition: background var(--t-fast) var(--ease), color var(--t-fast) var(--ease);
}
.tree-row:hover { background: rgba(255,255,255,.05); }
.tree-row.active {
  background: rgba(99,102,241,.16);
  color: #fff;
}
.tree-row.active::before {
  content: "";
  position: absolute;
  left: -6px; top: 6px; bottom: 6px;
  width: 2px;
  background: var(--accent);
  border-radius: 2px;
}

.tree-row .twist {
  width: 14px;
  flex-shrink: 0;
  text-align: center;
  color: var(--fg-on-dark-3);
  font-size: 9px;
  transition: transform var(--t-fast) var(--ease);
}
.tree-row.dir > .twist::before { content: "▶"; display: inline-block; transition: transform var(--t-fast) var(--ease); }
.tree-row.dir.open > .twist::before { transform: rotate(90deg); }
.tree-row.file > .twist { visibility: hidden; }

.tree-row .icon {
  width: 16px;
  flex-shrink: 0;
  text-align: center;
  font-size: 11px;
  color: var(--fg-on-dark-3);
}
.tree-row.dir .icon::before { content: ""; display: inline-block; width: 12px; height: 10px; background: var(--fg-on-dark-3); border-radius: 1px 3px 3px 3px; opacity: .55; vertical-align: middle; }
.tree-row.dir.open .icon::before { background: var(--accent); opacity: .85; }
.tree-row.file .icon::before { content: ""; display: inline-block; width: 9px; height: 11px; background: var(--fg-on-dark-3); border-radius: 1px; opacity: .45; vertical-align: middle; }
.tree-row.file.note .icon::before { background: var(--accent); opacity: .85; }

.tree-row .label {
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  font-weight: 400;
}
.tree-row.dir > .label { font-weight: 500; }
.tree-row.active .label { color: #fff; }

.tree-children { overflow: hidden; }

.tree-empty { padding: 18px 16px; color: var(--fg-on-dark-3); font-size: 12px; }

.focus-pill {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 6px 8px 6px;
  padding: 5px 6px 5px 9px;
  background: rgba(99,102,241,.16);
  border: 1px solid rgba(99,102,241,.32);
  border-radius: var(--r-xs);
  font-size: 12px;
  color: var(--fg-on-dark);
}
.focus-pill .focus-icon { color: var(--accent); font-size: 9px; flex-shrink: 0; }
.focus-pill .focus-label {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 500;
  cursor: pointer;
}
.focus-pill .focus-label:hover { color: #fff; }
.focus-pill .focus-clear {
  background: transparent;
  border: 0;
  color: var(--fg-on-dark-2);
  cursor: pointer;
  font-size: 11px;
  padding: 2px 5px;
  border-radius: 3px;
  flex-shrink: 0;
}
.focus-pill .focus-clear:hover { background: rgba(255,255,255,.12); color: #fff; }

/* ============================== Main ============================== */
#main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-canvas);
}

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 18px;
  height: 46px;
  background: var(--bg-topbar);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
  font-size: 13px;
  z-index: 5;
}

#breadcrumb {
  font-weight: 500;
  color: var(--fg);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}
#breadcrumb b { font-weight: 600; }
#breadcrumb .dim { color: var(--fg-muted); font-weight: 400; }
#breadcrumb .sep { color: var(--fg-muted); margin: 0 6px; opacity: .6; }

#status {
  color: var(--fg-muted);
  font-size: 12px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}
#status::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: transparent; transition: background var(--t-base); }
#status.saving::before { background: var(--warning); }
#status.saved::before  { background: var(--success); }
#status.error::before  { background: var(--danger);  }
#status.saving { color: var(--warning); }
#status.saved  { color: var(--success); }
#status.error  { color: var(--danger);  }

/* ============================== Format Toolbar ============================== */
#format-toolbar {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 6px 12px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
  flex-wrap: wrap;
  font-size: 13px;
  z-index: 4;
  position: relative;
  user-select: none;
}
#format-toolbar[hidden] { display: none; }

.ft-group { display: inline-flex; gap: 1px; }
.ft-sep   { width: 1px; height: 22px; background: var(--line); margin: 0 4px; }

.ft-btn {
  min-width: 30px;
  height: 30px;
  padding: 0 8px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--r-xs);
  color: var(--fg-2);
  cursor: pointer;
  font-family: inherit;
  font-size: 13px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  transition: background var(--t-fast), border-color var(--t-fast), color var(--t-fast);
}
.ft-btn b, .ft-btn i, .ft-btn u, .ft-btn s { font-weight: 600; font-size: 14px; }
.ft-btn:hover { background: var(--bg-canvas); color: var(--fg); }
.ft-btn:active { background: var(--accent-soft); }
.ft-btn.on { background: var(--accent-soft); color: var(--accent-hover); border-color: rgba(99,102,241,.2); }

.ft-select {
  height: 30px;
  padding: 0 26px 0 10px;
  border: 1px solid var(--line-strong);
  border-radius: var(--r-xs);
  background: var(--bg-card);
  background-image: linear-gradient(45deg, transparent 50%, var(--fg-muted) 50%),
                    linear-gradient(135deg, var(--fg-muted) 50%, transparent 50%);
  background-position: calc(100% - 14px) 50%, calc(100% - 10px) 50%;
  background-size: 4px 4px;
  background-repeat: no-repeat;
  font-family: inherit;
  font-size: 13px;
  color: var(--fg);
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  transition: border-color var(--t-fast);
}
.ft-select:hover { border-color: var(--fg-muted); }
.ft-select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(99,102,241,.15); }

.ft-color-btn { padding: 0 6px; flex-direction: column; gap: 0; line-height: 1; min-width: 32px; }
.ft-color-letter { font-size: 12px; font-weight: 600; height: 16px; display: flex; align-items: center; }
.ft-color-bar    { display: block; width: 18px; height: 4px; border-radius: 2px; margin-top: 1px; }

.ft-color-popup {
  position: absolute;
  z-index: 100;
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--r);
  box-shadow: var(--shadow-lg);
  padding: 10px;
  display: grid;
  grid-template-columns: repeat(8, 24px);
  gap: 4px;
}
.ft-color-popup[hidden] { display: none; }
.ft-color-popup button {
  width: 24px; height: 24px;
  border: 1px solid rgba(0,0,0,.10);
  border-radius: 4px;
  cursor: pointer;
  padding: 0;
  transition: transform var(--t-fast);
}
.ft-color-popup button:hover { transform: scale(1.15); }
.ft-color-popup .ft-color-clear {
  grid-column: span 8;
  width: auto; height: 26px;
  background: var(--bg-canvas);
  border: 1px solid var(--line-strong);
  margin-top: 4px;
  font-size: 12px;
}

.ft-table-popup {
  position: absolute;
  z-index: 100;
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--r);
  box-shadow: var(--shadow-lg);
  padding: 12px;
}
.ft-table-popup[hidden] { display: none; }
.ft-table-grid {
  display: grid;
  grid-template-columns: repeat(10, 22px);
  gap: 2px;
}
.ft-table-grid .cell {
  width: 22px; height: 22px;
  background: var(--bg-canvas);
  border: 1px solid var(--line-strong);
  border-radius: 2px;
  cursor: pointer;
  transition: background var(--t-fast);
}
.ft-table-grid .cell.hot { background: var(--accent-soft); border-color: var(--accent); }
.ft-table-label {
  font-size: 12px;
  color: var(--fg-muted);
  margin-top: 8px;
  font-variant-numeric: tabular-nums;
  text-align: center;
}

/* ============================== Canvas-Wrap ============================== */
#canvas-wrap {
  flex: 1;
  min-height: 0;
  position: relative;
  overflow: hidden;
  background: var(--bg-canvas);
}

#canvas {
  position: absolute;
  inset: 0;
  display: none;
  overflow: auto;
  background:
    radial-gradient(circle at 1px 1px, var(--dot) 1px, transparent 1.5px);
  background-size: 28px 28px;
}
#canvas.active { display: block; }
#canvas::-webkit-scrollbar { width: 10px; height: 10px; }
#canvas::-webkit-scrollbar-thumb { background: rgba(0,0,0,.10); border-radius: 5px; border: 2px solid var(--bg-canvas); }
#canvas::-webkit-scrollbar-thumb:hover { background: rgba(0,0,0,.18); }
#canvas::-webkit-scrollbar-track { background: transparent; }

#canvas-surface {
  position: relative;
  width: 4000px;
  height: 3000px;
}

/* ============================== Empty Hint ============================== */
#empty-hint {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 10px;
  color: var(--fg-muted);
  text-align: center;
  padding: 32px;
  pointer-events: none;
  z-index: 0;
}
#empty-hint .big { font-size: 17px; color: var(--fg-2); font-weight: 500; }
#empty-hint .small { font-size: 13px; max-width: 480px; line-height: 1.6; color: var(--fg-muted); }
#empty-hint .small b { color: var(--fg-2); font-weight: 600; }
#canvas.active ~ #empty-hint,
#viewer.active ~ #empty-hint { display: none; }

/* ============================== Boxes ============================== */
.box {
  position: absolute;
  min-width: 100px;
  min-height: 50px;
  background: var(--bg-card);
  border: 1px solid transparent;
  border-radius: var(--r-sm);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  font-family: var(--font-note);
  font-size: 14px;
  line-height: 1.55;
  color: var(--fg);
  transition: border-color var(--t-fast) var(--ease),
              box-shadow var(--t-fast) var(--ease),
              transform var(--t-fast) var(--ease);
}
.box:hover {
  border-color: var(--line-strong);
  box-shadow: var(--shadow);
}
.box.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(99,102,241,.15), var(--shadow);
}
.box.editing {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(99,102,241,.18), var(--shadow-md);
}
.box.dragging { opacity: .94; cursor: grabbing; }

.box .handle {
  height: 10px;
  flex-shrink: 0;
  cursor: grab;
  position: relative;
}
.box .handle::after {
  content: "";
  position: absolute;
  left: 50%; top: 4px;
  width: 24px; height: 2px;
  background: var(--line-strong);
  border-radius: 1px;
  transform: translateX(-50%);
  opacity: 0;
  transition: opacity var(--t-fast) var(--ease);
}
.box:hover .handle::after,
.box.selected .handle::after,
.box.editing .handle::after { opacity: .65; }

.box .body {
  flex: 1;
  padding: 10px 14px 12px;
  overflow: auto;
  cursor: text;
  word-wrap: break-word;
  scrollbar-width: thin;
  outline: none;
}
.box .body::-webkit-scrollbar { width: 6px; }
.box .body::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 3px; }
.box .body:empty::before {
  content: "Tippen für Inhalt …";
  color: var(--fg-muted);
  pointer-events: none;
}
.box .body[contenteditable=true]:focus { cursor: text; }

/* prose elements inside boxes */
.box .body > :first-child { margin-top: 0; }
.box .body > :last-child  { margin-bottom: 0; }
.box .body h1 { font-size: 1.55em; font-weight: 650; margin: .4em 0 .3em; letter-spacing: -.01em; }
.box .body h2 { font-size: 1.3em;  font-weight: 650; margin: .4em 0 .3em; letter-spacing: -.01em; }
.box .body h3 { font-size: 1.12em; font-weight: 650; margin: .4em 0 .25em; }
.box .body p  { margin: .3em 0; }
.box .body ul, .box .body ol { margin: .3em 0; padding-left: 1.6em; }
.box .body li { margin: .12em 0; }
.box .body ul.todo { list-style: none; padding-left: 1.4em; }
.box .body ul.todo > li { position: relative; padding-left: 4px; }
.box .body ul.todo > li::before {
  content: "";
  position: absolute;
  left: -1.3em;
  top: .35em;
  width: 14px; height: 14px;
  border: 1.5px solid var(--line-strong);
  border-radius: 3px;
  background: var(--bg-card);
  cursor: pointer;
}
.box .body ul.todo > li.done { color: var(--fg-muted); text-decoration: line-through; }
.box .body ul.todo > li.done::before {
  background: var(--accent);
  border-color: var(--accent);
}
.box .body ul.todo > li.done::after {
  content: "";
  position: absolute;
  left: -1em;
  top: .55em;
  width: 6px; height: 3px;
  border-left: 2px solid #fff;
  border-bottom: 2px solid #fff;
  transform: rotate(-45deg);
}
.box .body a {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px solid rgba(99,102,241,.3);
}
.box .body a:hover { color: var(--accent-hover); }
.box .body code {
  font-family: var(--font-mono);
  font-size: .9em;
  background: var(--bg-canvas);
  padding: 1px 6px;
  border-radius: var(--r-xs);
}
.box .body pre {
  background: #1a1a1c;
  color: #e4e4e7;
  padding: 12px 14px;
  border-radius: var(--r-sm);
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 12.5px;
  margin: .5em 0;
}
.box .body pre code { background: transparent; color: inherit; padding: 0; }
.box .body blockquote {
  margin: .5em 0;
  padding: 2px 0 2px 12px;
  border-left: 3px solid var(--accent-dark);
  color: var(--fg-2);
}
.box .body hr { border: 0; border-top: 1px solid var(--line); margin: .8em 0; }
.box .body table {
  border-collapse: collapse;
  margin: .5em 0;
  font-size: .95em;
}
.box .body th, .box .body td {
  border: 1px solid var(--line-strong);
  padding: 5px 9px;
  min-width: 40px;
}
.box .body th { background: var(--bg-canvas); font-weight: 600; }
.box .body img {
  max-width: 100%;
  height: auto;
  display: inline-block;
  vertical-align: middle;
  border-radius: var(--r-xs);
}
.box .body .file-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  background: var(--bg-canvas);
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  text-decoration: none !important;
  border-bottom: 1px solid var(--line) !important;
  color: var(--fg) !important;
  font-size: 13px;
  margin: 2px 0;
}
.box .body .file-link:hover { background: var(--accent-soft); border-color: var(--accent); }
.box .body .file-link::before {
  content: "📎";
  font-size: 11px;
}

.box .resize {
  position: absolute;
  right: 0; bottom: 0;
  width: 16px; height: 16px;
  cursor: nwse-resize;
  opacity: 0;
  transition: opacity var(--t-fast) var(--ease);
}
.box .resize::after {
  content: "";
  position: absolute;
  right: 3px; bottom: 3px;
  width: 8px; height: 8px;
  background:
    linear-gradient(135deg, transparent 0 40%, var(--fg-muted) 40% 55%, transparent 55% 70%, var(--fg-muted) 70% 85%, transparent 85%);
  border-radius: 0 0 var(--r-xs) 0;
}
.box:hover .resize, .box.selected .resize, .box.editing .resize { opacity: .8; }

.box .toolbar {
  position: absolute;
  top: -32px;
  right: 0;
  display: none;
  gap: 2px;
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  padding: 2px;
  box-shadow: var(--shadow);
}
.box:hover .toolbar, .box.selected .toolbar, .box.editing .toolbar { display: inline-flex; }
.box .toolbar button {
  background: transparent;
  border: 0;
  cursor: pointer;
  padding: 4px 8px;
  font-size: 14px;
  line-height: 1;
  color: var(--fg-muted);
  border-radius: var(--r-xs);
  transition: background var(--t-fast), color var(--t-fast);
}
.box .toolbar button:hover { background: var(--bg-canvas); color: var(--fg); }
.box .toolbar button.del:hover { background: #fee2e2; color: var(--danger); }

/* Rendered markdown inside .box */
.md > :first-child { margin-top: 0; }
.md > :last-child  { margin-bottom: 0; }
.md h1, .md h2, .md h3, .md h4 {
  font-family: var(--font-note);
  font-weight: 650;
  line-height: 1.25;
  margin: .55em 0 .3em;
  letter-spacing: -.01em;
}
.md h1 { font-size: 1.55em; }
.md h2 { font-size: 1.3em; }
.md h3 { font-size: 1.1em; }
.md h4 { font-size: 1em; color: var(--fg-2); }
.md p { margin: .35em 0; }
.md ul, .md ol { margin: .35em 0; padding-left: 1.6em; }
.md li { margin: .12em 0; }
.md a { color: var(--accent); text-decoration: none; border-bottom: 1px solid rgba(99,102,241,.3); }
.md a:hover { color: var(--accent-hover); border-bottom-color: var(--accent-hover); }
.md code {
  font-family: var(--font-mono);
  font-size: .9em;
  background: var(--bg-canvas);
  padding: 1px 6px;
  border-radius: var(--r-xs);
  border: 1px solid var(--line);
}
.md pre {
  background: #1a1a1c;
  color: #e4e4e7;
  padding: 12px 14px;
  border-radius: var(--r-sm);
  overflow-x: auto;
  font-size: 12.5px;
  line-height: 1.5;
  margin: .5em 0;
}
.md pre code { background: transparent; color: inherit; padding: 0; border: 0; }
.md blockquote {
  margin: .5em 0;
  padding: 2px 0 2px 14px;
  border-left: 3px solid var(--accent-dark);
  color: var(--fg-2);
}
.md hr { border: 0; border-top: 1px solid var(--line); margin: .8em 0; }
.md table { border-collapse: collapse; margin: .5em 0; font-size: .95em; }
.md th, .md td { border: 1px solid var(--line); padding: 5px 10px; }
.md th { background: var(--bg-canvas); font-weight: 600; }
.md input[type=checkbox] { margin-right: 6px; }

/* ============================== Viewer ============================== */
#viewer {
  position: absolute;
  inset: 0;
  display: none;
  background: var(--bg-canvas);
  z-index: 1;
}
#viewer.active { display: block; }

/* Every direct child fills the viewer absolutely — no flex+100% headaches. */
#viewer > * {
  position: absolute;
  inset: 0;
}

#viewer .v-frame {
  width: 100%;
  height: 100%;
  border: 0;
  display: block;
  background: #fff;
}

#viewer .v-img-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 28px;
  overflow: auto;
}
#viewer .v-img-wrap img {
  max-width: 100%;
  max-height: 100%;
  background: #fff;
  box-shadow: var(--shadow-lg);
  border-radius: var(--r-sm);
}

#viewer .v-center {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

#viewer .v-card {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--r);
  padding: 32px 32px;
  box-shadow: var(--shadow);
  text-align: center;
  max-width: 460px;
}
#viewer .v-card-title { font-weight: 600; margin-bottom: 6px; word-break: break-all; font-size: 15px; }
#viewer .v-card-sub   { color: var(--fg-muted); font-size: 13px; margin-bottom: 20px; }

#viewer .v-loading { display: flex; align-items: center; justify-content: center; color: var(--fg-muted); font-size: 14px; }
#viewer .v-error   { padding: 32px; color: var(--danger); }

#viewer .v-sheet { /* inherits absolute-fill from #viewer > * */ }
#viewer .v-tabs {
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 44px;
  display: flex; gap: 2px;
  padding: 8px 12px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
  z-index: 1;
}
#viewer .v-tabs button {
  background: transparent;
  border: 1px solid transparent;
  padding: 5px 12px;
  border-radius: var(--r-xs);
  font-size: 12px;
  color: var(--fg-muted);
  cursor: pointer;
  white-space: nowrap;
  transition: background var(--t-fast), color var(--t-fast), border-color var(--t-fast);
}
#viewer .v-tabs button:hover { background: var(--bg-canvas); color: var(--fg); }
#viewer .v-tabs button.on {
  background: var(--accent-soft);
  color: var(--accent-hover);
  border-color: rgba(99,102,241,.2);
  font-weight: 500;
}

#viewer .v-sheet-body {
  position: absolute;
  top: 44px; left: 0; right: 0; bottom: 0;
  overflow: auto;
  padding: 16px;
}
#viewer .v-sheet.no-tabs .v-sheet-body { top: 0; }

#viewer .v-table-wrap {
  overflow: auto;
  padding: 16px;
}
#viewer .v-table-wrap table {
  border-collapse: collapse;
  background: var(--bg-card);
  font-size: 13px;
  box-shadow: var(--shadow);
  border-radius: var(--r-sm);
  overflow: hidden;
}
#viewer .v-table-wrap th,
#viewer .v-table-wrap td,
#viewer .v-sheet-body th,
#viewer .v-sheet-body td {
  border: 1px solid var(--line);
  padding: 6px 12px;
  max-width: 360px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: var(--font-ui);
}
#viewer .v-table-wrap th,
#viewer .v-sheet-body th {
  background: var(--bg-canvas);
  font-weight: 600;
  position: sticky; top: 0;
  z-index: 1;
}
#viewer .v-sheet-body table {
  border-collapse: collapse;
  background: var(--bg-card);
  font-size: 13px;
  box-shadow: var(--shadow-sm);
}

#viewer .v-media {
  display: flex;
  align-items: center;
  justify-content: center;
  background: #0a0a0c;
  padding: 24px;
}
#viewer .v-media audio { width: min(560px, 100%); }
#viewer .v-media video { max-width: 100%; max-height: 100%; border-radius: var(--r-sm); box-shadow: var(--shadow-lg); }

/* DOCX viewer — paper metaphor, centered max-width column */
#viewer .v-doc {
  overflow: auto;
  padding: 28px 24px 96px;
  background: var(--bg-canvas);
}
#viewer .v-doc-paper {
  max-width: 780px;
  margin: 0 auto;
  background: var(--bg-card);
  padding: 64px 80px;
  box-shadow: var(--shadow-md);
  border-radius: var(--r-sm);
  font-family: var(--font-note);
  font-size: 14.5px;
  line-height: 1.7;
  color: var(--fg);
}
#viewer .v-doc-paper img { max-width: 100%; height: auto; display: block; margin: 1em auto; }
#viewer .v-doc-paper table { border-collapse: collapse; margin: 1em 0; }
#viewer .v-doc-paper th, #viewer .v-doc-paper td { border: 1px solid var(--line); padding: 6px 10px; }
#viewer .v-doc-paper h1 { font-size: 1.8em; margin: 0 0 .4em; }
#viewer .v-doc-paper h2 { font-size: 1.45em; margin: 1em 0 .35em; }
#viewer .v-doc-paper h3 { font-size: 1.2em; margin: .85em 0 .3em; }
#viewer .v-doc-paper p  { margin: .55em 0; }

/* Code viewer — line numbers + monospace pane */
#viewer .v-code {
  display: flex;
  flex-direction: column;
  background: var(--bg-card);
}
#viewer .v-code-header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 36px;
  padding: 0 14px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--line);
  font-size: 12px;
  color: var(--fg-muted);
}
#viewer .v-code-lang {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--accent-hover);
  background: var(--accent-soft);
  padding: 2px 8px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: .04em;
  font-weight: 600;
}
#viewer .v-code-stats { font-variant-numeric: tabular-nums; }
#viewer .v-code-body {
  position: absolute;
  top: 36px; left: 0; right: 0; bottom: 0;
  display: flex;
  overflow: auto;
  background: var(--bg-card);
}
#viewer .v-code-ln,
#viewer .v-code-src {
  margin: 0;
  padding: 14px 0;
  font-family: var(--font-mono);
  font-size: 12.5px;
  line-height: 1.6;
  white-space: pre;
  tab-size: 4;
}
#viewer .v-code-ln {
  padding-left: 14px;
  padding-right: 14px;
  color: var(--fg-muted);
  text-align: right;
  user-select: none;
  border-right: 1px solid var(--line);
  background: var(--bg-canvas);
  position: sticky;
  left: 0;
  z-index: 1;
}
#viewer .v-code-src {
  padding-left: 16px;
  padding-right: 24px;
  color: var(--fg);
  flex: 1;
  min-width: 0;
}

#viewer .v-card-actions {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}
#viewer .v-card-actions .btn-primary,
#viewer .v-card-actions .btn-secondary {
  text-decoration: none;
  display: inline-block;
}

/* ---------- Font preview ---------- */
#viewer .v-font {
  overflow: auto;
  background: var(--bg-card);
}
#viewer .v-font-header {
  padding: 28px 36px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--bg-canvas);
  font-family: var(--font-ui);
}
#viewer .v-font-title { font-size: 20px; font-weight: 600; }
#viewer .v-font-sub   { font-size: 13px; color: var(--fg-muted); margin-top: 4px; }
#viewer .v-font-body  { padding: 28px 36px 80px; }
#viewer .v-font-line  { margin: 4px 0 18px; line-height: 1.15; color: var(--fg); word-break: break-word; }
#viewer .v-font-pangram {
  margin-top: 24px;
  padding: 22px 26px;
  background: var(--bg-canvas);
  border-radius: var(--r-sm);
  border: 1px solid var(--line);
  white-space: pre-wrap;
  font-size: 22px;
  line-height: 1.5;
  outline: none;
}
#viewer .v-font-pangram:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(99,102,241,.15); }

/* ---------- Data tables (ICS / VCF / GPX / archive) ---------- */
#viewer .v-data {
  overflow: auto;
  padding: 28px 36px 80px;
  background: var(--bg-canvas);
  font-family: var(--font-ui);
}
#viewer .v-data > h2 { margin: 0 0 4px; font-size: 19px; font-weight: 600; }
#viewer .v-data > h3 { margin: 28px 0 10px; font-size: 14px; font-weight: 600; color: var(--fg-2); text-transform: uppercase; letter-spacing: .04em; }
#viewer .v-data-sub { font-size: 13px; color: var(--fg-muted); margin-bottom: 16px; }
#viewer .v-data-table {
  border-collapse: collapse;
  width: 100%;
  background: var(--bg-card);
  font-size: 13px;
  box-shadow: var(--shadow-sm);
  border-radius: var(--r-sm);
  overflow: hidden;
}
#viewer .v-data-table th {
  background: var(--bg-canvas);
  padding: 8px 14px;
  text-align: left;
  font-weight: 600;
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0;
  z-index: 1;
}
#viewer .v-data-table td {
  padding: 7px 14px;
  border-top: 1px solid var(--line);
  max-width: 380px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
#viewer .v-data-table tbody tr:hover td { background: var(--bg-canvas); }

/* ---------- Hex dump (binary fallback) ---------- */
#viewer .v-binary {
  overflow: auto;
  padding: 28px 36px 80px;
  background: var(--bg-canvas);
}
#viewer .v-bin-card {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--r);
  padding: 22px 26px;
  box-shadow: var(--shadow-sm);
  margin-bottom: 18px;
}
#viewer .v-bin-title { font-size: 16px; font-weight: 600; margin-bottom: 4px; word-break: break-all; }
#viewer .v-bin-sub   { font-size: 13px; color: var(--fg-muted); margin-bottom: 14px; }
#viewer .v-bin-sub b { color: var(--fg); font-weight: 600; }
#viewer .v-bin-actions { display: flex; gap: 8px; flex-wrap: wrap; }
#viewer .v-bin-actions a { text-decoration: none; }
#viewer .v-hex {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--r);
  padding: 16px 18px;
  box-shadow: var(--shadow-sm);
}
#viewer .v-hex-meta {
  display: flex;
  gap: 24px;
  font-size: 12px;
  margin-bottom: 12px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--line);
  color: var(--fg-muted);
}
#viewer .v-hex-meta b { color: var(--fg); font-weight: 600; }
#viewer .v-hex pre {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.55;
  color: var(--fg-2);
  white-space: pre;
  overflow-x: auto;
}
#viewer .v-hex pre .off { color: var(--fg-muted); }
#viewer .v-hex pre .asc { color: var(--accent); }

/* ---------- EPUB reader ---------- */
#viewer .v-epub {
  background: var(--bg-canvas);
}
#viewer .v-epub-toolbar {
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--line);
  font-size: 13px;
  color: var(--fg-2);
}
#viewer .v-epub-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-right: 16px; }
#viewer .v-epub-nav   { display: flex; gap: 6px; flex-shrink: 0; }
#viewer .v-epub-nav .btn-secondary { padding: 5px 12px; font-size: 12px; }
#viewer .v-epub-reader {
  position: absolute;
  top: 44px; left: 0; right: 0; bottom: 0;
  overflow: hidden;
  background: var(--bg-card);
}

/* ============================== Context Menu ============================== */
.ctx {
  position: fixed;
  z-index: 1000;
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--r);
  box-shadow: var(--shadow-lg);
  padding: 5px;
  min-width: 192px;
  font-size: 13px;
  animation: ctx-in 120ms var(--ease);
}
@keyframes ctx-in {
  from { opacity: 0; transform: translateY(-3px) scale(.98); }
  to   { opacity: 1; transform: translateY(0)   scale(1); }
}
.ctx button {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  padding: 7px 12px;
  cursor: pointer;
  border-radius: var(--r-xs);
  color: var(--fg);
  font-size: 13px;
  font-family: inherit;
  transition: background var(--t-fast);
}
.ctx button:hover { background: var(--bg-canvas); }
.ctx button.danger { color: var(--danger); }
.ctx button.danger:hover { background: #fef2f2; }
.ctx .sep { height: 1px; background: var(--line); margin: 4px 4px; }

/* ============================== Modal ============================== */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 15, 17, .42);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  animation: overlay-in 160ms var(--ease);
}
.overlay[hidden] { display: none; }
@keyframes overlay-in { from { opacity: 0; } to { opacity: 1; } }

.modal {
  background: var(--bg-card);
  border-radius: var(--r);
  padding: 22px 24px 20px;
  width: 400px;
  max-width: calc(100vw - 32px);
  box-shadow: var(--shadow-lg);
  animation: modal-in 200ms var(--ease);
}
@keyframes modal-in {
  from { opacity: 0; transform: translateY(8px) scale(.98); }
  to   { opacity: 1; transform: translateY(0)   scale(1); }
}
.modal-title { font-weight: 600; margin-bottom: 12px; font-size: 14px; }
.modal-input {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--line-strong);
  border-radius: var(--r-sm);
  font-size: 14px;
  font-family: var(--font-ui);
  color: var(--fg);
  background: var(--bg-canvas);
  outline: none;
  transition: border-color var(--t-fast), background var(--t-fast), box-shadow var(--t-fast);
}
.modal-input:focus {
  border-color: var(--accent);
  background: var(--bg-card);
  box-shadow: 0 0 0 3px rgba(99,102,241,.15);
}
.modal-msg { font-size: 12px; color: var(--fg-muted); margin-top: 10px; min-height: 1em; }
.modal-msg.error { color: var(--danger); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

.btn-primary, .btn-secondary {
  border: 1px solid transparent;
  border-radius: var(--r-sm);
  padding: 7px 16px;
  font-size: 13px;
  font-family: inherit;
  font-weight: 500;
  cursor: pointer;
  transition: background var(--t-fast), border-color var(--t-fast), color var(--t-fast), box-shadow var(--t-fast);
}
.btn-primary {
  background: var(--accent);
  color: white;
  box-shadow: 0 1px 2px rgba(99,102,241,.3), inset 0 1px 0 rgba(255,255,255,.16);
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:active { transform: translateY(.5px); }

.btn-secondary {
  background: var(--bg-card);
  border-color: var(--line-strong);
  color: var(--fg-2);
}
.btn-secondary:hover { background: var(--bg-canvas); border-color: var(--fg-muted); color: var(--fg); }
