/* Fermivie — tokens, shell, shared controls. Plain desktop look (see CONTRACT.md §6). */
:root {
  color-scheme: light;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;

  --bg: #ffffff;            /* content panes, table */
  --bg-side: #f4f4f3;       /* sidebars */
  --bg-bar: #ececea;        /* tab bar, toolbars */
  --bg-input: #ffffff;
  --bg-sunken: #f7f7f6;
  --row-alt: #f8f8f7;
  --hover: rgba(0, 0, 0, .06);
  --active: rgba(0, 0, 0, .1);
  --line: #d4d4d1;
  --line-soft: #e5e5e2;
  --text: #1d1d1f;
  --text-2: #5c5f66;
  --text-3: #8c9099;
  --accent: #2462d0;
  --accent-text: #1f56b8;
  --sel: #2462d0;
  --sel-text: #ffffff;
  --sel-weak: #d9e3f5;
  --focus: #3b78e7;
  --danger: #b3261e;
  --ok: #217a45;
  --warn: #8a5a00;
  --paper-bg: #d8d8d5;     /* behind PDF pages */
  --shadow-pop: 0 10px 28px rgba(0, 0, 0, .18), 0 0 0 1px rgba(0, 0, 0, .08);

  /* mark colours come from js/marktypes.js (class mt-<type>); these are fallbacks */
  --q: #d49a12;  --q-bg: rgba(212, 154, 18, .26);
  --r: #cf5683;  --r-bg: rgba(207, 86, 131, .26);
  --e: #2a9c85;  --e-bg: rgba(42, 156, 133, .26);
  --mt: #7b8595; --mt-ink: #4c5463; --mt-tag: #4c5463; --mt-wash: rgba(123,133,149,.26); --mt-soft: rgba(123,133,149,.08);
  --ctl-shadow: 0 1px 0 rgba(0, 0, 0, .04);

  /* sizes (Settings > Appearance > Density) */
  --tab-h: 34px; --bar-h: 36px; --row-h: 24px; --ctl-h: 26px;
  --vtabs-w: 220px;
}
/* dark: follows the system unless Settings forces a theme (<html data-theme="light|dark">) */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #1e1e1e;
    --bg-side: #252525;
    --bg-bar: #2b2b2b;
    --bg-input: #1a1a1a;
    --bg-sunken: #222;
    --row-alt: #222222;
    --hover: rgba(255, 255, 255, .07);
    --active: rgba(255, 255, 255, .12);
    --line: #3b3b3b;
    --line-soft: #313131;
    --text: #e6e6e6;
    --text-2: #a7a9ad;
    --text-3: #7d8086;
    --accent: #5b95f0;
    --accent-text: #7aa9f3;
    --sel: #2f63c4;
    --sel-text: #ffffff;
    --sel-weak: #2a3a55;
    --focus: #5b95f0;
    --danger: #f08070;
    --ok: #63c08a;
    --warn: #e0b050;
    --paper-bg: #3a3a3a;
    --shadow-pop: 0 12px 30px rgba(0, 0, 0, .5), 0 0 0 1px rgba(255, 255, 255, .08);
    --ctl-shadow: none;
    --mt-ink: #aeb6c3;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #1e1e1e;
  --bg-side: #252525;
  --bg-bar: #2b2b2b;
  --bg-input: #1a1a1a;
  --bg-sunken: #222;
  --row-alt: #222222;
  --hover: rgba(255, 255, 255, .07);
  --active: rgba(255, 255, 255, .12);
  --line: #3b3b3b;
  --line-soft: #313131;
  --text: #e6e6e6;
  --text-2: #a7a9ad;
  --text-3: #7d8086;
  --accent: #5b95f0;
  --accent-text: #7aa9f3;
  --sel: #2f63c4;
  --sel-text: #ffffff;
  --sel-weak: #2a3a55;
  --focus: #5b95f0;
  --danger: #f08070;
  --ok: #63c08a;
  --warn: #e0b050;
  --paper-bg: #3a3a3a;
  --shadow-pop: 0 12px 30px rgba(0, 0, 0, .5), 0 0 0 1px rgba(255, 255, 255, .08);
  --ctl-shadow: none;
  --mt-ink: #aeb6c3;
}
:root[data-theme="light"] { color-scheme: light; }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 13px/1.4 var(--font); overflow: hidden; -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: default; }
a { color: var(--accent-text); text-decoration: none; }
a:hover { text-decoration: underline; }
kbd { font: 11px var(--mono); color: var(--text-3); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--text-2); }
.faint { color: var(--text-3); }
.grow { flex: 1; }

/* ------------------------------------------------------------------ shell */
#app { height: 100%; display: flex; flex-direction: column; }
.view { flex: 1; min-height: 0; min-width: 0; display: flex; flex-direction: column; }

/* tab bar (top). Settings > General > Tabs can move it down the left side: see "vertical tabs" below */
.tabbar { height: var(--tab-h); flex: none; display: flex; align-items: stretch; background: var(--bg-bar); border-bottom: 1px solid var(--line); }
.tabbar__list { flex: 1; min-width: 0; display: flex; align-items: stretch; overflow-x: auto; overflow-y: hidden; scrollbar-width: none; }
.tabbar__list::-webkit-scrollbar { display: none; }
.tabbar__searchbtn { flex: none; align-self: center; margin: 0 4px 0 2px; }
.tabbar__more { flex: none; align-self: center; margin-left: 2px; }
.tab { position: relative; display: flex; align-items: center; gap: 7px; flex: none; min-width: 0; max-width: 240px; padding: 0 8px 0 12px; border: 0; border-right: 1px solid var(--line); background: transparent; color: var(--text-2); white-space: nowrap; }
.tab:hover { background: var(--hover); color: var(--text); }
.tab[aria-selected="true"] { background: var(--bg); color: var(--text); }
.tab--library { padding-right: 14px; font-weight: 600; }
.tab--settings { padding: 0 11px; }
.tab__text { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.tab__label { overflow: hidden; text-overflow: ellipsis; }
.tab--paper { flex: 0 1 auto; min-width: 104px; }      /* top bar: paper tabs shrink before the strip scrolls */
.tab__ver { margin-right: 3px; font-size: 12px; color: var(--text-3); }
.tab[aria-selected="true"] .tab__ver { color: var(--text-2); }
.tab__sub { display: none; }
.tab__close { width: 18px; height: 18px; flex: none; display: grid; place-items: center; border: 0; border-radius: 3px; background: transparent; color: var(--text-3); padding: 0; }
.tab__close:hover { background: var(--active); color: var(--text); }
.tab svg.ic { width: 15px; height: 15px; flex: none; }

/* ------------------------------------------------------------------ controls */
.ic { width: 16px; height: 16px; display: block; stroke: currentColor; fill: none; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }
.btn { height: var(--ctl-h); display: inline-flex; align-items: center; gap: 6px; padding: 0 10px; border: 1px solid var(--line); border-radius: 5px; background: var(--bg-input); color: var(--text); white-space: nowrap; box-shadow: var(--ctl-shadow); font-weight: 500; }
.btn .ic { width: 15px; height: 15px; color: var(--text-2); }
.btn--primary .ic { color: inherit; }
.btn:hover { background: var(--bg-sunken); }
.btn:active { background: var(--active); }
.btn:disabled { opacity: .5; }
.btn--primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn--primary:hover { background: var(--accent); filter: brightness(1.06); }
.btn--danger { color: var(--danger); }
.btn--small { height: 22px; padding: 0 8px; font-size: 12px; }
.ibtn { width: 28px; height: var(--ctl-h); display: inline-grid; place-items: center; border: 1px solid transparent; border-radius: 5px; background: transparent; color: var(--text-2); padding: 0; }
.ibtn:hover { background: var(--hover); color: var(--text); }
.ibtn:active { background: var(--active); color: var(--text); }
.ibtn[aria-pressed="true"] { background: var(--bg-input); border-color: var(--line); color: var(--text); box-shadow: var(--ctl-shadow); }

/* segmented control: related buttons sharing one frame (zoom, tool switch) */
.segctl { display: inline-flex; align-items: stretch; height: var(--ctl-h); border: 1px solid var(--line); border-radius: 5px; background: var(--bg-input); box-shadow: var(--ctl-shadow); }
.segctl > .zoomwrap { display: flex; }
.segctl__btn { display: inline-flex; align-items: center; gap: 5px; height: 100%; padding: 0 9px; border: 0; border-left: 1px solid var(--line-soft); background: transparent; color: var(--text-2); font-weight: 500; white-space: nowrap; }
.segctl > :first-child, .segctl > :first-child .segctl__btn { border-left: 0; border-radius: 4px 0 0 4px; }
.segctl > :last-child { border-radius: 0 4px 4px 0; }
.segctl__btn:hover { background: var(--hover); color: var(--text); }
.segctl__btn--icon { padding: 0 6px; }
.segctl__btn .ic { width: 15px; height: 15px; }
.segctl__btn[aria-checked="true"] { background: var(--active); color: var(--text); }
.ibtn:disabled { opacity: .4; }
.input, .select, .textarea { height: var(--ctl-h); padding: 0 8px; border: 1px solid var(--line); border-radius: 4px; background: var(--bg-input); color: var(--text); }
.input:focus, .select:focus, .textarea:focus { outline: none; border-color: var(--focus); box-shadow: 0 0 0 2px color-mix(in srgb, var(--focus) 25%, transparent); }
.textarea { height: auto; padding: 6px 8px; line-height: 1.45; resize: vertical; }
.select { padding-right: 22px; appearance: none; background-image: linear-gradient(45deg, transparent 50%, var(--text-3) 50%), linear-gradient(135deg, var(--text-3) 50%, transparent 50%); background-position: calc(100% - 12px) 55%, calc(100% - 8px) 55%; background-size: 4px 4px; background-repeat: no-repeat; }
.search { position: relative; display: flex; align-items: center; }
.search .ic { position: absolute; left: 7px; width: 14px; height: 14px; color: var(--text-3); pointer-events: none; }
.search .input { padding-left: 26px; width: 100%; }
.toolbar { height: var(--bar-h); flex: none; display: flex; align-items: center; gap: 4px; padding: 0 8px; background: var(--bg-bar); border-bottom: 1px solid var(--line); }
.toolbar__sep { width: 1px; height: 18px; background: var(--line); margin: 0 4px; }
.splitter { width: 5px; margin: 0 -2px; flex: none; cursor: col-resize; position: relative; z-index: 5; }
.splitter:hover, .splitter.dragging { background: color-mix(in srgb, var(--focus) 35%, transparent); }
.sq { display: inline-block; width: 3px; height: 12px; flex: none; background: var(--mt); }
.tlabel { color: var(--mt-ink); font-weight: 600; }
.empty { color: var(--text-2); padding: 28px 20px; text-align: center; line-height: 1.5; }

/* ------------------------------------------------------------------ menus, popovers, dialogs */
#overlay-root { position: fixed; inset: 0; pointer-events: none; z-index: 1000; }
#overlay-root > * { pointer-events: auto; }
.menu { position: fixed; min-width: 180px; max-width: 320px; max-height: calc(100vh - 16px); overflow-y: auto; padding: 4px; border-radius: 6px; background: var(--bg); box-shadow: var(--shadow-pop); font-size: 13px; outline: none; }
.menu__item { display: flex; align-items: center; gap: 8px; width: 100%; height: 24px; padding: 0 10px 0 8px; border: 0; border-radius: 4px; background: transparent; text-align: left; color: var(--text); white-space: nowrap; }
.menu__item:disabled { color: var(--text-3); }
.menu__item.is-active:not(:disabled) { background: var(--sel); color: var(--sel-text); }
.menu__item.is-active:not(:disabled) kbd, .menu__item.is-active:not(:disabled) .menu__chev { color: inherit; opacity: .8; }
.menu__check { width: 14px; flex: none; text-align: center; }
.menu__label { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.menu__chev { color: var(--text-3); }
.menu__sep { height: 1px; margin: 4px 6px; background: var(--line-soft); }
.menu__head { padding: 4px 10px 2px 30px; font-size: 12px; color: var(--text-3); }
.popover { position: fixed; padding: 12px; border-radius: 6px; background: var(--bg); box-shadow: var(--shadow-pop); }
.scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, .25); display: grid; place-items: center; }
.dialog { width: min(420px, calc(100vw - 32px)); padding: 18px 18px 14px; border-radius: 8px; background: var(--bg); box-shadow: var(--shadow-pop); }
.dialog h2 { margin: 0 0 6px; font-size: 14px; font-weight: 600; }
.dialog p { margin: 0 0 12px; color: var(--text-2); line-height: 1.45; }
.dialog .input, .dialog .textarea { width: 100%; margin-bottom: 12px; }
.dialog__actions { display: flex; justify-content: flex-end; gap: 8px; }

.toast { position: fixed; left: 50%; bottom: 18px; transform: translateX(-50%); z-index: 1100; max-width: min(560px, calc(100vw - 32px)); padding: 7px 12px; border-radius: 6px; background: #2b2b2b; color: #f2f2f2; font-size: 12.5px; box-shadow: var(--shadow-pop); }
.toast.is-error { background: #6b1d17; }

.dropzone { position: fixed; inset: 0; z-index: 900; display: grid; place-items: center; background: color-mix(in srgb, var(--accent) 12%, transparent); border: 2px dashed var(--accent); pointer-events: none; }
.dropzone__box { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 16px 22px; border-radius: 8px; background: var(--bg); box-shadow: var(--shadow-pop); }
.dropzone__box span { color: var(--text-2); font-size: 12px; }

@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

/* ------------------------------------------------------------------ mark types editor */
.mte-editor { width: min(720px, calc(100vw - 32px)); padding: 0; display: flex; flex-direction: column; max-height: calc(100vh - 48px); }
.mte-head { padding: 16px 18px 10px; border-bottom: 1px solid var(--line-soft); }
.mte-head h2 { margin: 0 0 4px; font-size: 14px; font-weight: 600; }
.mte-head p { margin: 0; color: var(--text-2); font-size: 12.5px; line-height: 1.45; }
.mte-body { display: flex; min-height: 0; flex: 1; }
.mte-side { width: 210px; flex: none; display: flex; flex-direction: column; border-right: 1px solid var(--line-soft); background: var(--bg-side); }
.mte-list { flex: 1; overflow: auto; padding: 6px; }
.mte-row { display: flex; align-items: center; gap: 8px; width: 100%; height: 28px; padding: 0 8px 0 0; border: 0; border-radius: 4px; background: transparent; text-align: left; color: var(--text); }
.mte-row:hover { background: var(--hover); }
.mte-row[aria-selected="true"] { background: var(--sel-weak); }
.mte-row__rule { width: 3px; align-self: stretch; margin: 5px 0; background: var(--mt); border-radius: 0; }
.mte-row__name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--mt-ink); font-weight: 600; }
.mte-row__meta { font-size: 11.5px; color: var(--text-3); }
.mte-sidefoot { padding: 8px; border-top: 1px solid var(--line-soft); }
.mte-form { flex: 1; overflow: auto; padding: 14px 18px; display: flex; flex-direction: column; gap: 12px; }
.mte-field { display: flex; flex-direction: column; gap: 4px; }
.mte-field__lbl { font-size: 12px; font-weight: 600; color: var(--text-2); }
.mte-field__hint { font-size: 11.5px; color: var(--text-3); line-height: 1.4; }
.mte-field .input, .mte-field .textarea { width: 100%; }
.mte-two { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.mte-swatches { display: flex; gap: 6px; flex-wrap: wrap; }
.mte-swatch { width: 26px; height: 20px; border: 1px solid var(--line); border-radius: 4px; padding: 3px; background: var(--bg-input); }
.mte-swatch::before { content: ""; display: block; width: 100%; height: 100%; border-radius: 2px; background: var(--sw); }
.mte-swatch[aria-checked="true"] { border-color: var(--text); box-shadow: 0 0 0 1px var(--text); }
.mte-checks { display: flex; flex-direction: column; gap: 6px; font-size: 13px; }
.mte-check { display: flex; align-items: center; gap: 6px; }
.mte-formfoot { display: flex; gap: 6px; align-items: center; padding-top: 4px; }
.mte-err { margin: 0; color: var(--danger); font-size: 12.5px; }
.mte-editor .dialog__actions { padding: 10px 18px 14px; border-top: 1px solid var(--line-soft); align-items: center; }
@media (max-width: 640px) { .mte-body { flex-direction: column; } .mte-side { width: auto; max-height: 160px; border-right: 0; border-bottom: 1px solid var(--line-soft); } .mte-two { grid-template-columns: 1fr; } }

/* keyboard shortcuts sheet */
.dialog--keys { width: min(560px, calc(100vw - 32px)); }
.keys { display: grid; grid-template-columns: max-content 1fr; gap: 5px 16px; margin: 10px 0 14px; font-size: 12.5px; max-height: 60vh; overflow: auto; }
.keys__head { grid-column: 1 / -1; font-weight: 600; color: var(--text-2); margin-top: 8px; padding-bottom: 3px; border-bottom: 1px solid var(--line-soft); }
.keys__k { font: 12px var(--mono); color: var(--text); white-space: nowrap; }
.keys__v { color: var(--text-2); }

.tab.is-dragging { opacity: .5; }
.tab.is-drop-before { box-shadow: inset 2px 0 0 var(--accent); }
.tab.is-drop-after { box-shadow: inset -2px 0 0 var(--accent); }

/* ------------------------------------------------------------------ vertical tabs (<html data-tabs="vertical">) */
:root[data-tabs="vertical"] #app { flex-direction: row; }
.tabbar--v { position: relative; width: var(--vtabs-w); height: auto; flex-direction: column; border-bottom: 0; border-right: 1px solid var(--line); background: var(--bg-side); }
.tabbar--v .tabbar__list { flex-direction: column; overflow-x: hidden; overflow-y: auto; padding: 2px 6px 6px; gap: 1px; scrollbar-width: thin; }
.tabbar--v .tab { max-width: none; min-width: 0; width: 100%; flex: none; gap: 8px; min-height: 28px; padding: 4px 4px 4px 8px; border: 0; border-radius: 4px; color: var(--text); text-align: left; }
.tabbar--v .tab:hover { background: var(--hover); }
.tabbar--v .tab[aria-selected="true"] { background: var(--sel-weak); color: var(--text); }
.tabbar--v .tab--library { padding-right: 8px; font-weight: 400; }
.tabbar--v .tab--paper { align-items: flex-start; }
.tabbar--v .tab--paper > svg.ic { margin-top: 1px; color: var(--text-3); }
.tabbar--v .tab__label { white-space: normal; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-height: 1.3; overflow-wrap: anywhere; }
.tabbar--v .tab--fixed .tab__label { display: block; white-space: nowrap; }
.tabbar--v .tab__sub { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; color: var(--text-3); }
.tabbar--v .tab__close { visibility: hidden; margin-left: auto; }
.tabbar--v .tab:hover .tab__close, .tabbar--v .tab[aria-selected="true"] .tab__close, .tabbar--v .tab:focus-visible .tab__close { visibility: visible; }
.tabbar--v .tab.is-drop-before { box-shadow: inset 0 2px 0 var(--accent); }
.tabbar--v .tab.is-drop-after { box-shadow: inset 0 -2px 0 var(--accent); }
.tabbar__sep { flex: none; height: 1px; margin: 5px 4px; background: var(--line-soft); }
.tabbar__top { flex: none; height: var(--bar-h); display: flex; align-items: center; padding: 0 6px; margin-bottom: 4px; background: var(--bg-bar); border-bottom: 1px solid var(--line); }
.tabbar__search { flex: 1; min-width: 0; display: flex; align-items: center; gap: 7px; height: var(--ctl-h); padding: 0 8px; border: 1px solid var(--line); border-radius: 5px; background: var(--bg-input); color: var(--text-3); text-align: left; box-shadow: var(--ctl-shadow); }
.tabbar__search:hover { color: var(--text-2); }
.tabbar__search .ic { width: 14px; height: 14px; flex: none; }
.tabbar__searchlabel { flex: 1; }
.tabbar__foot { flex: none; display: flex; justify-content: flex-end; padding: 4px 6px; border-top: 1px solid var(--line-soft); }
.tabbar__splitter { position: absolute; top: 0; bottom: 0; right: -3px; width: 6px; cursor: col-resize; z-index: 6; }
.tabbar__splitter:hover, .tabbar__splitter.dragging { background: color-mix(in srgb, var(--focus) 35%, transparent); }
/* narrowed to icons */
.tabbar--v.is-min { width: 46px; }
.tabbar--v.is-min .tabbar__list { padding: 2px 5px 6px; }
.tabbar--v.is-min .tab { justify-content: center; padding: 6px 0; }
.tabbar--v.is-min .tab__text, .tabbar--v.is-min .tab__label, .tabbar--v.is-min .tab__close,
.tabbar--v.is-min .tabbar__searchlabel, .tabbar--v.is-min .tabbar__search kbd { display: none; }
.tabbar--v.is-min .tabbar__top { padding: 0 5px; }
.tabbar--v.is-min .tabbar__search { justify-content: center; padding: 0; }
.tabbar--v.is-min .tabbar__foot { justify-content: center; }
.tabbar--v.is-min-preview { opacity: .6; }

/* ------------------------------------------------------------------ compact density (<html data-density="compact">) */
:root[data-density="compact"] { --tab-h: 29px; --bar-h: 31px; --row-h: 20px; --ctl-h: 22px; }
:root[data-density="compact"] .menu__item { height: 22px; }
:root[data-density="compact"] .lib-row, :root[data-density="compact"] .lib-thead, :root[data-density="compact"] .coll-row { height: var(--row-h); }
:root[data-density="compact"] .tabbar--v .tab { min-height: 24px; padding-top: 2px; padding-bottom: 2px; }
