/*
 * Luna — components.
 *
 * Hand-written, organised with custom properties from theme.css. No framework, no
 * preprocessor, no utility-class library, nothing fetched from a CDN.
 *
 * Layout: a fixed left navigation rail (collapsible to icon-only) and a top bar with the
 * global search, theme toggle and lock button. Under 768px the rail becomes a bottom tab
 * bar, dialogs become full-screen sheets and tables become stacked cards. The mobile
 * layout is a first-class target — this is a PWA that gets installed on a phone.
 */

/* ---------------------------------------------------------------- reset + base */

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    background: var(--luna-bg);
    color: var(--luna-text);
    font-family: var(--font);
    font-size: var(--fs-md);
    line-height: 1.5;
    min-height: 100vh;
}

.luna-sprite { display: none; }

h1, h2, h3, h4 { margin: 0; font-weight: 600; line-height: 1.25; }
h1 { font-size: var(--fs-2xl); }
h2 { font-size: var(--fs-xl); }
h3 { font-size: var(--fs-lg); }
p  { margin: 0 0 var(--sp-3); }
ul, ol { margin: 0; padding: 0; }

a { color: var(--luna-accent); text-decoration: none; }
a:hover { text-decoration: underline; }

img { max-width: 100%; }

::selection { background: var(--luna-selection); }

:focus-visible {
    outline: 2px solid var(--luna-accent);
    outline-offset: 2px;
}

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

.skip-link {
    position: absolute;
    left: -9999px;
    top: 0;
    z-index: 100;
    background: var(--luna-surface);
    color: var(--luna-text);
    padding: var(--sp-2) var(--sp-4);
    border: 1px solid var(--luna-border);
}
.skip-link:focus { left: var(--sp-2); top: var(--sp-2); }

.icon {
    width: 20px;
    height: 20px;
    flex: none;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.6;
    stroke-linecap: round;
    stroke-linejoin: round;
    vertical-align: middle;
}
.icon-sm { width: 16px; height: 16px; }
.icon-lg { width: 26px; height: 26px; }

/* ---------------------------------------------------------------- app shell */

.app {
    display: grid;
    grid-template-columns: var(--rail-width) 1fr;
    grid-template-rows: var(--topbar-height) 1fr;
    grid-template-areas: "rail topbar" "rail main";
    min-height: 100vh;
}
.app.rail-collapsed { grid-template-columns: var(--rail-width-collapsed) 1fr; }

/* --- navigation rail --- */

.rail {
    grid-area: rail;
    background: var(--luna-surface);
    border-right: 1px solid var(--luna-border);
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
    overscroll-behavior: contain;
}

.rail-brand {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    height: var(--topbar-height);
    padding: 0 var(--sp-3);
    border-bottom: 1px solid var(--luna-border);
    flex: none;
}
.rail-mark {
    width: 28px; height: 28px;
    display: grid;
    place-items: center;
    background: var(--luna-purple);
    color: #fff;
    font-weight: 700;
    border-radius: var(--radius-tile);
    flex: none;
    font-size: var(--fs-lg);
}
.rail-name { font-weight: 600; font-size: var(--fs-lg); letter-spacing: .2px; }

.rail-nav { padding: var(--sp-2); display: flex; flex-direction: column; gap: 2px; list-style: none; }
.rail-link {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    padding: var(--sp-2) var(--sp-3);
    border-radius: var(--radius);
    color: var(--luna-text);
    border-left: 3px solid transparent;
    min-height: 40px;
    text-decoration: none;
}
.rail-link:hover { background: var(--luna-surface-2); text-decoration: none; }
.rail-link[aria-current="page"] {
    background: var(--luna-surface-2);
    border-left-color: var(--luna-accent);
    font-weight: 600;
}
.rail-link .count {
    margin-left: auto;
    font-size: var(--fs-xs);
    color: var(--luna-text-muted);
    background: var(--luna-surface-3);
    border-radius: 10px;
    padding: 1px 7px;
    font-variant-numeric: tabular-nums;
}
.rail-foot { margin-top: auto; padding: var(--sp-2); border-top: 1px solid var(--luna-border); }

.app.rail-collapsed .rail-name,
.app.rail-collapsed .rail-link span:not(.count),
.app.rail-collapsed .rail-link .count { display: none; }
.app.rail-collapsed .rail-link { justify-content: center; padding: var(--sp-2); }

/* --- top bar --- */

.topbar {
    grid-area: topbar;
    background: var(--luna-surface);
    border-bottom: 1px solid var(--luna-border);
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    padding: 0 var(--sp-3);
    position: sticky;
    top: 0;
    z-index: 30;
}
.topbar-search {
    position: relative;
    flex: 1 1 auto;
    max-width: 520px;
}
.topbar-search .icon {
    position: absolute;
    left: var(--sp-2);
    top: 50%;
    transform: translateY(-50%);
    color: var(--luna-text-faint);
    pointer-events: none;
}
/* Typed with the attribute so it outweighs the generic input rule further down,
   which would otherwise put the placeholder underneath the search icon. */
.topbar-search input[type="search"],
.topbar-search input[type="text"] { padding-left: 34px; }
.topbar-actions { display: flex; align-items: center; gap: var(--sp-1); margin-left: auto; }

.search-results {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    background: var(--luna-surface);
    border: 1px solid var(--luna-border);
    border-radius: var(--radius);
    box-shadow: var(--elev-3);
    max-height: 60vh;
    overflow-y: auto;
    z-index: 40;
    padding: var(--sp-1);
}
.search-results:empty { display: none; }
.search-result {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    padding: var(--sp-2);
    border-radius: var(--radius);
    color: var(--luna-text);
}
.search-result:hover, .search-result:focus { background: var(--luna-surface-2); text-decoration: none; }
.search-result .kind {
    font-size: var(--fs-xs);
    text-transform: uppercase;
    letter-spacing: .4px;
    color: var(--luna-text-faint);
    flex: none;
    width: 64px;
}
.search-result .title { font-weight: 500; }
.search-result .sub { color: var(--luna-text-muted); font-size: var(--fs-sm); margin-left: auto; }
.search-empty { padding: var(--sp-3); color: var(--luna-text-muted); }

/* --- main --- */

.main {
    grid-area: main;
    padding: var(--sp-5);
    max-width: var(--content-max);
    width: 100%;
}
.main.main-flush { padding: 0; max-width: none; }

.page-head {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    margin-bottom: var(--sp-4);
    flex-wrap: wrap;
}
.page-head h1 { margin-right: auto; }
.page-sub { color: var(--luna-text-muted); font-size: var(--fs-sm); }

/* ---------------------------------------------------------------- controls */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-2);
    min-height: 34px;
    padding: 0 var(--sp-3);
    border: 1px solid var(--luna-border-strong);
    border-radius: var(--radius);
    background: var(--luna-surface);
    color: var(--luna-text);
    font: inherit;
    font-size: var(--fs-md);
    cursor: pointer;
    text-decoration: none;
    transition: background var(--speed) linear, border-color var(--speed) linear;
}
.btn:hover { background: var(--luna-surface-2); text-decoration: none; }
.btn:active { background: var(--luna-surface-3); }
.btn[disabled], .btn[aria-disabled="true"] { opacity: .5; cursor: not-allowed; }

.btn-primary {
    background: var(--luna-accent);
    border-color: var(--luna-accent);
    color: var(--luna-on-accent);
    font-weight: 600;
}
.btn-primary:hover { background: var(--luna-accent-hover); border-color: var(--luna-accent-hover); }

.btn-danger { color: var(--luna-danger); border-color: var(--luna-danger); }
.btn-danger:hover { background: var(--luna-danger-bg); }

.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: var(--luna-surface-2); }

.btn-icon {
    width: 36px;
    min-height: 36px;
    padding: 0;
    border-color: transparent;
    background: transparent;
}
.btn-icon:hover { background: var(--luna-surface-2); }

.btn-sm { min-height: 28px; font-size: var(--fs-sm); padding: 0 var(--sp-2); }
.btn-block { width: 100%; }

.btn-group { display: inline-flex; gap: 0; }
.btn-group .btn { border-radius: 0; margin-left: -1px; }
.btn-group .btn:first-child { border-radius: var(--radius) 0 0 var(--radius); margin-left: 0; }
.btn-group .btn:last-child { border-radius: 0 var(--radius) var(--radius) 0; }
.btn-group .btn[aria-pressed="true"] { background: var(--luna-accent-soft); border-color: var(--luna-accent); }

input[type="text"], input[type="search"], input[type="password"], input[type="number"],
input[type="url"], input[type="email"], textarea, select {
    width: 100%;
    min-height: 34px;
    padding: var(--sp-1) var(--sp-2);
    border: 1px solid var(--luna-border-strong);
    border-radius: var(--radius);
    background: var(--luna-surface);
    color: var(--luna-text);
    font: inherit;
    font-size: var(--fs-md);
}
textarea { min-height: 120px; resize: vertical; line-height: 1.6; }
input:focus, textarea:focus, select:focus {
    outline: none;
    border-color: var(--luna-accent);
    box-shadow: 0 0 0 1px var(--luna-accent);
}
input::placeholder, textarea::placeholder { color: var(--luna-text-faint); }

label { display: block; font-size: var(--fs-sm); font-weight: 600; margin-bottom: var(--sp-1); }
.field { margin-bottom: var(--sp-3); }
.field-hint { font-size: var(--fs-xs); color: var(--luna-text-muted); margin-top: var(--sp-1); font-weight: 400; }
.field-error { font-size: var(--fs-xs); color: var(--luna-danger); margin-top: var(--sp-1); }
.field-row { display: flex; gap: var(--sp-3); flex-wrap: wrap; }
.field-row > .field { flex: 1 1 200px; }

.check {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    font-size: var(--fs-md);
    font-weight: 400;
    margin-bottom: var(--sp-2);
    cursor: pointer;
    min-height: 32px;
}
.check input { width: 16px; height: 16px; min-height: 0; accent-color: var(--luna-accent); flex: none; }

.radio-row { display: flex; gap: var(--sp-4); flex-wrap: wrap; }

/* ---------------------------------------------------------------- surfaces */

.card {
    background: var(--luna-surface);
    border: 1px solid var(--luna-border);
    border-radius: var(--radius);
    box-shadow: var(--elev-1);
}
.card-pad { padding: var(--sp-4); }
.card + .card { margin-top: var(--sp-4); }

.panel {
    background: var(--luna-surface);
    border: 1px solid var(--luna-border);
    border-radius: var(--radius);
    padding: var(--sp-4);
    margin-bottom: var(--sp-4);
}
.panel-head {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    margin-bottom: var(--sp-3);
}
.panel-head h2, .panel-head h3 { margin-right: auto; }

hr {
    border: none;
    border-top: 1px solid var(--luna-border);
    margin: var(--sp-4) 0;
}

.empty {
    text-align: center;
    padding: var(--sp-6) var(--sp-4);
    color: var(--luna-text-muted);
}
.empty .icon { width: 40px; height: 40px; color: var(--luna-text-faint); margin-bottom: var(--sp-3); }

.banner {
    display: flex;
    align-items: flex-start;
    gap: var(--sp-3);
    padding: var(--sp-3);
    border-radius: var(--radius);
    border: 1px solid var(--luna-border);
    border-left: 3px solid var(--luna-info);
    background: var(--luna-info-bg);
    margin-bottom: var(--sp-4);
    font-size: var(--fs-sm);
}
.banner-warn   { border-left-color: var(--luna-warn);   background: var(--luna-warn-bg); }
.banner-danger { border-left-color: var(--luna-danger); background: var(--luna-danger-bg); }
.banner-ok     { border-left-color: var(--luna-ok);     background: var(--luna-ok-bg); }
.banner a { font-weight: 600; }

/* ---------------------------------------------------------------- Metro tiles */

.tiles {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
    gap: var(--sp-3);
    margin-bottom: var(--sp-5);
}
.tile {
    position: relative;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    min-height: 132px;
    padding: var(--sp-4);
    border-radius: var(--radius-tile);
    background: var(--accent, var(--luna-blue));
    color: #fff;
    border: none;
    text-decoration: none;
    transition: transform var(--speed) ease-out, box-shadow var(--speed) ease-out;
}
.tile:hover {
    transform: translateY(-2px);
    box-shadow: var(--elev-2);
    text-decoration: none;
    color: #fff;
}
.tile .icon { width: 26px; height: 26px; opacity: .95; }
.tile-count { font-size: var(--fs-3xl); font-weight: 600; line-height: 1; font-variant-numeric: tabular-nums; }
.tile-label { font-size: var(--fs-lg); font-weight: 600; }
.tile-snippet {
    font-size: var(--fs-sm);
    opacity: .92;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.tile-wide { grid-column: span 2; }

.stat-row { display: flex; flex-wrap: wrap; gap: var(--sp-3); margin-bottom: var(--sp-5); }
.stat {
    flex: 1 1 140px;
    background: var(--luna-surface);
    border: 1px solid var(--luna-border);
    border-radius: var(--radius);
    padding: var(--sp-3);
}
.stat-value { font-size: var(--fs-xl); font-weight: 600; font-variant-numeric: tabular-nums; }
.stat-label { font-size: var(--fs-sm); color: var(--luna-text-muted); }

/* ---------------------------------------------------------------- lists */

.recent-list { list-style: none; }
.recent-item {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    padding: var(--sp-2) var(--sp-1);
    border-bottom: 1px solid var(--luna-border);
    color: var(--luna-text);
}
.recent-item:last-child { border-bottom: none; }
.recent-item:hover { background: var(--luna-surface-2); text-decoration: none; }
.recent-item .icon { color: var(--accent-ink, var(--luna-text-muted)); }
.recent-item .meta { margin-left: auto; color: var(--luna-text-faint); font-size: var(--fs-xs); white-space: nowrap; }
.recent-item .title { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recent-item .sub { color: var(--luna-text-muted); font-size: var(--fs-sm); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* --- tag chips --- */

.chips { display: flex; flex-wrap: wrap; gap: var(--sp-1); align-items: center; }
.chip {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: var(--fs-xs);
    padding: 2px 8px;
    border-radius: 2px;
    border: 1px solid var(--accent-ink, var(--luna-border-strong));
    color: var(--accent-ink, var(--luna-text-muted));
    background: transparent;
    white-space: nowrap;
    max-width: 160px;
    overflow: hidden;
    text-overflow: ellipsis;
}
a.chip:hover { background: var(--luna-surface-2); text-decoration: none; }
.chip.is-active {
    background: var(--accent-ink, var(--luna-accent));
    border-color: var(--accent-ink, var(--luna-accent));
    color: var(--luna-surface);
    font-weight: 600;
}
.chip.is-active .faint { color: inherit; opacity: .8; }
.chip-remove {
    border: none;
    background: none;
    color: inherit;
    cursor: pointer;
    padding: 0;
    line-height: 1;
    display: inline-flex;
}
.chip-input { min-width: 120px; border: none; outline: none; background: transparent; color: inherit; min-height: 26px; }
.chip-editor {
    display: flex;
    flex-wrap: wrap;
    gap: var(--sp-1);
    align-items: center;
    border: 1px solid var(--luna-border-strong);
    border-radius: var(--radius);
    padding: var(--sp-1);
    background: var(--luna-surface);
}
.chip-editor:focus-within { border-color: var(--luna-accent); box-shadow: 0 0 0 1px var(--luna-accent); }

.tag-suggest {
    position: absolute;
    z-index: 60;
    background: var(--luna-surface);
    border: 1px solid var(--luna-border);
    border-radius: var(--radius);
    box-shadow: var(--elev-2);
    max-height: 220px;
    overflow-y: auto;
    min-width: 180px;
}
.tag-suggest button {
    display: block;
    width: 100%;
    text-align: left;
    border: none;
    background: none;
    color: var(--luna-text);
    padding: var(--sp-2) var(--sp-3);
    cursor: pointer;
    font: inherit;
}
.tag-suggest button:hover, .tag-suggest button.active { background: var(--luna-surface-2); }

/* --- sidebar filter --- */

.with-sidebar {
    display: grid;
    grid-template-columns: 240px 1fr;
    gap: var(--sp-4);
    align-items: start;
}
.filter-sidebar {
    background: var(--luna-surface);
    border: 1px solid var(--luna-border);
    border-radius: var(--radius);
    padding: var(--sp-3);
    position: sticky;
    top: calc(var(--topbar-height) + var(--sp-3));
    max-height: calc(100vh - var(--topbar-height) - var(--sp-5));
    overflow-y: auto;
}
.filter-group + .filter-group { margin-top: var(--sp-4); }
.filter-title {
    font-size: var(--fs-xs);
    text-transform: uppercase;
    letter-spacing: .5px;
    color: var(--luna-text-faint);
    margin-bottom: var(--sp-2);
    display: flex;
    align-items: center;
    gap: var(--sp-2);
}
.filter-title .btn { margin-left: auto; }
.filter-list { list-style: none; display: flex; flex-direction: column; gap: 1px; }
.filter-link {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    padding: 5px var(--sp-2);
    border-radius: var(--radius);
    color: var(--luna-text);
    font-size: var(--fs-sm);
    min-height: 30px;
}
.filter-link:hover { background: var(--luna-surface-2); text-decoration: none; }
.filter-link.is-active { background: var(--luna-accent-soft); font-weight: 600; }
.filter-link .dot { width: 8px; height: 8px; border-radius: 1px; background: var(--accent-ink, var(--luna-gray)); flex: none; }
.filter-link .count { margin-left: auto; color: var(--luna-text-faint); font-variant-numeric: tabular-nums; }
.filter-link .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.toolbar {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    flex-wrap: wrap;
    margin-bottom: var(--sp-3);
}
.toolbar .spacer { margin-left: auto; }
.toolbar form { display: contents; }

/* ---------------------------------------------------------------- notes */

.notes-layout {
    display: grid;
    grid-template-columns: 320px 1fr;
    gap: var(--sp-4);
    align-items: start;
}
.note-list {
    list-style: none;
    background: var(--luna-surface);
    border: 1px solid var(--luna-border);
    border-radius: var(--radius);
    max-height: calc(100vh - var(--topbar-height) - 120px);
    overflow-y: auto;
}
.note-item {
    display: block;
    padding: var(--sp-3);
    border-bottom: 1px solid var(--luna-border);
    border-left: 3px solid transparent;
    color: var(--luna-text);
}
.note-item:last-child { border-bottom: none; }
.note-item:hover { background: var(--luna-surface-2); text-decoration: none; }
.note-item.is-active { background: var(--luna-surface-2); border-left-color: var(--accent-ink, var(--luna-accent)); }
.note-item .row { display: flex; align-items: center; gap: var(--sp-2); }
.note-item .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.note-item .pin { color: var(--luna-orange-ink); margin-left: auto; }
.note-item .excerpt {
    color: var(--luna-text-muted);
    font-size: var(--fs-sm);
    margin-top: 2px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.note-item .meta { color: var(--luna-text-faint); font-size: var(--fs-xs); margin-top: var(--sp-1); }

.note-editor {
    background: var(--luna-surface);
    border: 1px solid var(--luna-border);
    border-radius: var(--radius);
    padding: var(--sp-4);
    min-height: 60vh;
    display: flex;
    flex-direction: column;
}
.note-editor-head { display: flex; align-items: center; gap: var(--sp-2); margin-bottom: var(--sp-3); }
.note-title-input {
    font-size: var(--fs-xl);
    font-weight: 600;
    border: none;
    background: transparent;
    padding: 0;
    min-height: 40px;
}
.note-title-input:focus { box-shadow: none; border-bottom: 1px solid var(--luna-accent); }
.note-body { flex: 1 1 auto; display: flex; gap: var(--sp-4); min-height: 320px; }
.note-body textarea {
    flex: 1 1 50%;
    font-family: var(--font-mono);
    font-size: var(--fs-md);
    height: auto;
    min-height: 320px;
}
.note-preview {
    flex: 1 1 50%;
    overflow-y: auto;
    border: 1px solid var(--luna-border);
    border-radius: var(--radius);
    padding: var(--sp-3);
    background: var(--luna-surface-2);
    max-height: 60vh;
}
.note-preview.is-only { flex-basis: 100%; }
.note-editor-foot {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    margin-top: var(--sp-3);
    padding-top: var(--sp-3);
    border-top: 1px solid var(--luna-border);
    flex-wrap: wrap;
}
.save-state { font-size: var(--fs-sm); color: var(--luna-text-muted); }
.save-state[data-state="dirty"] { color: var(--luna-warn); }
.save-state[data-state="saved"] { color: var(--luna-ok); }
.save-state[data-state="error"] { color: var(--luna-danger); }

.color-picker { display: flex; gap: var(--sp-1); }
.color-swatch {
    width: 22px; height: 22px;
    border-radius: var(--radius-tile);
    border: 1px solid var(--luna-border-strong);
    background: var(--accent, var(--luna-surface-3));
    cursor: pointer;
    padding: 0;
}
.color-swatch[aria-pressed="true"] { outline: 2px solid var(--luna-text); outline-offset: 1px; }

/* rendered markdown */
.markdown { line-height: 1.65; overflow-wrap: anywhere; }
.markdown h1, .markdown h2, .markdown h3 { margin: var(--sp-4) 0 var(--sp-2); }
.markdown h1 { font-size: var(--fs-xl); }
.markdown h2 { font-size: var(--fs-lg); }
.markdown p { margin: 0 0 var(--sp-3); }
.markdown ul, .markdown ol { padding-left: var(--sp-5); margin: 0 0 var(--sp-3); }
.markdown li { margin-bottom: var(--sp-1); }
.markdown code {
    font-family: var(--font-mono);
    font-size: var(--fs-sm);
    background: var(--luna-surface-3);
    padding: 1px 4px;
    border-radius: 2px;
}
.markdown pre {
    background: var(--luna-surface-3);
    padding: var(--sp-3);
    border-radius: var(--radius);
    overflow-x: auto;
}
.markdown pre code { background: none; padding: 0; }
.markdown blockquote {
    margin: 0 0 var(--sp-3);
    padding-left: var(--sp-3);
    border-left: 3px solid var(--luna-border-strong);
    color: var(--luna-text-muted);
}
.markdown table { border-collapse: collapse; width: 100%; margin-bottom: var(--sp-3); }
.markdown th, .markdown td { border: 1px solid var(--luna-border); padding: var(--sp-1) var(--sp-2); text-align: left; }
.markdown img { border-radius: var(--radius); }
.markdown hr { border: none; border-top: 1px solid var(--luna-border); margin: var(--sp-4) 0; }

/* ---------------------------------------------------------------- bookmarks */

.bookmark-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: var(--sp-3);
}
.bookmark-card {
    background: var(--luna-surface);
    border: 1px solid var(--luna-border);
    border-radius: var(--radius);
    padding: var(--sp-3);
    display: flex;
    flex-direction: column;
    gap: var(--sp-2);
    position: relative;
    transition: border-color var(--speed) linear, box-shadow var(--speed) linear;
}
.bookmark-card:hover { border-color: var(--luna-border-strong); box-shadow: var(--elev-1); }
.bookmark-card.is-selected { border-color: var(--luna-accent); box-shadow: 0 0 0 1px var(--luna-accent); }
.bookmark-head { display: flex; align-items: flex-start; gap: var(--sp-2); }
.bookmark-title {
    font-weight: 600;
    color: var(--luna-text);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    overflow-wrap: anywhere;
}
.bookmark-title:hover { color: var(--luna-accent); }
.bookmark-host { font-size: var(--fs-xs); color: var(--luna-text-faint); overflow-wrap: anywhere; }
.bookmark-desc { font-size: var(--fs-sm); color: var(--luna-text-muted); }
.bookmark-foot { display: flex; align-items: center; gap: var(--sp-1); margin-top: auto; }
.bookmark-actions { display: flex; gap: 2px; margin-left: auto; }

.favicon {
    width: 24px; height: 24px;
    border-radius: 2px;
    flex: none;
    object-fit: contain;
    background: var(--luna-surface-3);
}
.letter-tile {
    width: 24px; height: 24px;
    border-radius: 2px;
    flex: none;
    display: grid;
    place-items: center;
    background: var(--accent, var(--luna-gray));
    color: #fff;
    font-size: var(--fs-sm);
    font-weight: 700;
}

.status-dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex: none; }
.status-ok { background: var(--luna-ok); }
.status-broken { background: var(--luna-danger); }
.status-timeout { background: var(--luna-warn); }
.status-blocked { background: var(--luna-gray); }

.select-box { flex: none; width: 16px; height: 16px; accent-color: var(--luna-accent); }

/* table view */
.table-wrap { overflow-x: auto; border: 1px solid var(--luna-border); border-radius: var(--radius); background: var(--luna-surface); }
table.data { width: 100%; border-collapse: collapse; font-size: var(--fs-sm); }
table.data th, table.data td { padding: var(--sp-2) var(--sp-3); text-align: left; border-bottom: 1px solid var(--luna-border); }
table.data th {
    font-size: var(--fs-xs);
    text-transform: uppercase;
    letter-spacing: .4px;
    color: var(--luna-text-faint);
    background: var(--luna-surface-2);
    position: sticky;
    top: 0;
    z-index: 1;
}
table.data tbody tr:hover { background: var(--luna-surface-2); }
table.data tr:last-child td { border-bottom: none; }
table.data td.numeric { text-align: right; font-variant-numeric: tabular-nums; }
table.data .row-actions { display: flex; gap: 2px; justify-content: flex-end; }

.bulk-bar {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    padding: var(--sp-2) var(--sp-3);
    background: var(--luna-accent-soft);
    border: 1px solid var(--luna-accent);
    border-radius: var(--radius);
    margin-bottom: var(--sp-3);
    flex-wrap: wrap;
    position: sticky;
    top: calc(var(--topbar-height) + var(--sp-1));
    z-index: 20;
}
.bulk-bar .count { font-weight: 600; }

.load-more { display: flex; justify-content: center; padding: var(--sp-4); }
.sentinel { height: 1px; }

.progress {
    height: 6px;
    background: var(--luna-surface-3);
    border-radius: 3px;
    overflow: hidden;
}
.progress-bar {
    height: 100%;
    width: 0;
    background: var(--luna-accent);
    transition: width 200ms linear;
}

/* ---------------------------------------------------------------- accounts */

.account-list { display: flex; flex-direction: column; gap: var(--sp-2); }
.account-row {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    background: var(--luna-surface);
    border: 1px solid var(--luna-border);
    border-radius: var(--radius);
    padding: var(--sp-3);
    flex-wrap: wrap;
}
.account-row:hover { border-color: var(--luna-border-strong); }
.account-main { flex: 1 1 220px; min-width: 0; }
.account-name { font-weight: 600; }
.account-user { color: var(--luna-text-muted); font-size: var(--fs-sm); overflow-wrap: anywhere; }
.account-secret {
    display: flex;
    align-items: center;
    gap: var(--sp-1);
    font-family: var(--font-mono);
    font-size: var(--fs-sm);
    background: var(--luna-surface-2);
    border: 1px solid var(--luna-border);
    border-radius: var(--radius);
    padding: 2px var(--sp-2);
    min-width: 170px;
    min-height: 32px;
}
.account-secret .value { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; letter-spacing: 1px; }
.account-actions { display: flex; gap: 2px; margin-left: auto; }
.clipboard-countdown { font-size: var(--fs-xs); color: var(--luna-text-muted); font-variant-numeric: tabular-nums; }

.strength { display: flex; align-items: center; gap: var(--sp-2); margin-top: var(--sp-1); }
.strength-track { flex: 1 1 auto; height: 4px; background: var(--luna-surface-3); border-radius: 2px; overflow: hidden; }
.strength-fill { height: 100%; width: 0; transition: width var(--speed) linear; }
.strength-0 .strength-fill { width: 20%; background: var(--luna-danger); }
.strength-1 .strength-fill { width: 40%; background: var(--luna-danger); }
.strength-2 .strength-fill { width: 60%; background: var(--luna-warn); }
.strength-3 .strength-fill { width: 80%; background: var(--luna-info); }
.strength-4 .strength-fill { width: 100%; background: var(--luna-ok); }
.strength-label { font-size: var(--fs-xs); color: var(--luna-text-muted); min-width: 70px; }

.generator { border: 1px solid var(--luna-border); border-radius: var(--radius); padding: var(--sp-3); background: var(--luna-surface-2); }
.generator-row { display: flex; align-items: center; gap: var(--sp-2); flex-wrap: wrap; }
.generator input[type="range"] { flex: 1 1 120px; accent-color: var(--luna-accent); }
.generator-out {
    font-family: var(--font-mono);
    background: var(--luna-surface);
    border: 1px solid var(--luna-border);
    border-radius: var(--radius);
    padding: var(--sp-2);
    overflow-wrap: anywhere;
    min-height: 38px;
}

/* ---------------------------------------------------------------- import */

.dropzone {
    border: 2px dashed var(--luna-border-strong);
    border-radius: var(--radius);
    padding: var(--sp-6) var(--sp-4);
    text-align: center;
    background: var(--luna-surface-2);
    cursor: pointer;
    transition: border-color var(--speed) linear, background var(--speed) linear;
}
.dropzone:hover, .dropzone.is-over { border-color: var(--luna-accent); background: var(--luna-accent-soft); }
.dropzone .icon { width: 32px; height: 32px; color: var(--luna-text-faint); margin-bottom: var(--sp-2); }
.dropzone-name { font-weight: 600; margin-top: var(--sp-2); }

.preview-stats { display: flex; flex-wrap: wrap; gap: var(--sp-2); margin-bottom: var(--sp-3); }
.preview-stat {
    background: var(--luna-surface-2);
    border: 1px solid var(--luna-border);
    border-radius: var(--radius);
    padding: var(--sp-2) var(--sp-3);
    min-width: 96px;
}
.preview-stat .value { font-size: var(--fs-lg); font-weight: 600; font-variant-numeric: tabular-nums; }
.preview-stat .label { font-size: var(--fs-xs); color: var(--luna-text-muted); }

.pill {
    display: inline-block;
    font-size: var(--fs-xs);
    padding: 1px 6px;
    border-radius: 2px;
    border: 1px solid currentColor;
}
.pill-insert { color: var(--luna-ok); }
.pill-update { color: var(--luna-info); }
.pill-skip   { color: var(--luna-text-faint); }

/* ---------------------------------------------------------------- dialogs, toasts */

.dialog-backdrop {
    position: fixed;
    inset: 0;
    background: var(--luna-overlay);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--sp-4);
    z-index: 80;
}
.dialog {
    background: var(--luna-surface);
    border: 1px solid var(--luna-border);
    border-radius: var(--radius);
    box-shadow: var(--elev-3);
    width: min(560px, 100%);
    max-height: calc(100vh - var(--sp-6));
    display: flex;
    flex-direction: column;
}
.dialog-wide { width: min(840px, 100%); }
.dialog-head {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    padding: var(--sp-3) var(--sp-4);
    border-bottom: 1px solid var(--luna-border);
}
.dialog-head h2 { margin-right: auto; font-size: var(--fs-lg); }
.dialog-body { padding: var(--sp-4); overflow-y: auto; }
.dialog-foot {
    display: flex;
    gap: var(--sp-2);
    justify-content: flex-end;
    padding: var(--sp-3) var(--sp-4);
    border-top: 1px solid var(--luna-border);
    flex-wrap: wrap;
}
.dialog-foot .left { margin-right: auto; }

.toasts {
    position: fixed;
    right: var(--sp-4);
    bottom: var(--sp-4);
    display: flex;
    flex-direction: column;
    gap: var(--sp-2);
    z-index: 90;
    max-width: min(380px, calc(100vw - var(--sp-5)));
}
.toast {
    display: flex;
    align-items: flex-start;
    gap: var(--sp-2);
    background: var(--luna-surface);
    border: 1px solid var(--luna-border);
    border-left: 3px solid var(--luna-accent);
    border-radius: var(--radius);
    box-shadow: var(--elev-2);
    padding: var(--sp-3);
    font-size: var(--fs-sm);
    animation: toast-in 160ms ease-out;
}
.toast-success { border-left-color: var(--luna-ok); }
.toast-error   { border-left-color: var(--luna-danger); }
.toast-warn    { border-left-color: var(--luna-warn); }
.toast .close { margin-left: auto; }

@keyframes toast-in {
    from { opacity: 0; transform: translateY(8px); }
    to   { opacity: 1; transform: none; }
}

/* ---------------------------------------------------------------- login */

.login-shell {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: var(--sp-4);
    padding-bottom: calc(var(--sp-4) + env(safe-area-inset-bottom));
}
.login-card {
    width: min(400px, 100%);
    background: var(--luna-surface);
    border: 1px solid var(--luna-border);
    border-radius: var(--radius);
    box-shadow: var(--elev-2);
    padding: var(--sp-5);
}
.login-brand { display: flex; align-items: center; gap: var(--sp-3); margin-bottom: var(--sp-5); }
.login-brand .rail-mark { width: 40px; height: 40px; font-size: var(--fs-xl); }
.login-brand h1 { font-size: var(--fs-xl); }
.login-brand .sub { color: var(--luna-text-muted); font-size: var(--fs-sm); }
.pin-input {
    font-size: var(--fs-xl);
    letter-spacing: 6px;
    text-align: center;
    min-height: 48px;
    font-family: var(--font-mono);
}

/* ---------------------------------------------------------------- settings */

.settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: var(--sp-4); align-items: start; }
.kv { display: grid; grid-template-columns: minmax(140px, 40%) 1fr; gap: var(--sp-1) var(--sp-3); font-size: var(--fs-sm); }
.kv dt { color: var(--luna-text-muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.kv .ok   { color: var(--luna-ok); font-weight: 600; }
.kv .bad  { color: var(--luna-danger); font-weight: 600; }

.tag-admin { display: flex; flex-direction: column; gap: var(--sp-1); max-height: 340px; overflow-y: auto; }
.tag-admin-row {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    padding: var(--sp-1) var(--sp-2);
    border-radius: var(--radius);
}
.tag-admin-row:hover { background: var(--luna-surface-2); }
.tag-admin-row .usage { margin-left: auto; color: var(--luna-text-faint); font-size: var(--fs-xs); }

/* ---------------------------------------------------------------- offline / misc */

.offline-indicator {
    display: none;
    align-items: center;
    gap: var(--sp-1);
    color: var(--luna-warn);
    font-size: var(--fs-sm);
}
body.is-offline .offline-indicator { display: inline-flex; }

.idle-warning {
    position: fixed;
    left: 50%;
    bottom: var(--sp-4);
    transform: translateX(-50%);
    background: var(--luna-surface);
    border: 1px solid var(--luna-warn);
    border-radius: var(--radius);
    box-shadow: var(--elev-3);
    padding: var(--sp-3) var(--sp-4);
    z-index: 95;
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    font-size: var(--fs-sm);
}

.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.muted { color: var(--luna-text-muted); }
.faint { color: var(--luna-text-faint); }
.nowrap { white-space: nowrap; }
.mono { font-family: var(--font-mono); }
.tabular { font-variant-numeric: tabular-nums; }
.mt-0 { margin-top: 0; }
.mt-2 { margin-top: var(--sp-2); }
.mt-3 { margin-top: var(--sp-3); }
.mt-4 { margin-top: var(--sp-4); }
.mb-0 { margin-bottom: 0; }
.mb-2 { margin-bottom: var(--sp-2); }
.mb-3 { margin-bottom: var(--sp-3); }
.mb-4 { margin-bottom: var(--sp-4); }
.flex { display: flex; gap: var(--sp-2); align-items: center; }
.flex-wrap { flex-wrap: wrap; }
.grow { flex: 1 1 auto; }
.hidden { display: none !important; }

/* ---------------------------------------------------------------- responsive */

@media (max-width: 1080px) {
    .with-sidebar { grid-template-columns: 200px 1fr; }
    .notes-layout { grid-template-columns: 280px 1fr; }
}

@media (max-width: 900px) {
    .note-body { flex-direction: column; }
    .note-preview { max-height: 320px; }
}

@media (max-width: 768px) {
    .app,
    .app.rail-collapsed {
        grid-template-columns: 1fr;
        grid-template-rows: var(--topbar-height) 1fr auto;
        grid-template-areas: "topbar" "main" "rail";
    }

    /* The rail becomes a bottom tab bar. `top: auto` is essential: the desktop rule
       above sets top: 0, and a fixed element with both top and bottom would stretch
       across the whole viewport and cover the page. */
    .rail {
        position: fixed;
        top: auto;
        left: 0; right: 0; bottom: 0;
        height: auto;
        border-right: none;
        border-top: 1px solid var(--luna-border);
        flex-direction: row;
        z-index: 50;
        padding-bottom: env(safe-area-inset-bottom);
        overflow: visible;
    }
    .rail-brand, .rail-foot { display: none; }
    .rail-nav { flex-direction: row; width: 100%; justify-content: space-around; padding: var(--sp-1); }
    .rail-link {
        flex-direction: column;
        gap: 2px;
        border-left: none;
        border-top: 3px solid transparent;
        font-size: var(--fs-xs);
        padding: var(--sp-1);
        min-width: 44px;
        min-height: 48px;
        justify-content: center;
    }
    .rail-link[aria-current="page"] { border-left-color: transparent; border-top-color: var(--luna-accent); }
    .rail-link .count { display: none; }

    .main { padding: var(--sp-3); padding-bottom: 84px; }

    .with-sidebar, .notes-layout { grid-template-columns: 1fr; }
    .filter-sidebar { position: static; max-height: none; }
    .note-list { max-height: 40vh; }

    /* Dialogs become full-screen sheets. */
    .dialog-backdrop { padding: 0; align-items: stretch; }
    .dialog, .dialog-wide {
        width: 100%;
        max-height: 100vh;
        border-radius: 0;
        border: none;
        padding-bottom: env(safe-area-inset-bottom);
    }

    .toasts { right: var(--sp-2); left: var(--sp-2); bottom: 76px; max-width: none; }

    /* Tables become stacked cards. */
    table.data thead { display: none; }
    table.data, table.data tbody, table.data tr, table.data td { display: block; width: 100%; }
    table.data tr {
        border: 1px solid var(--luna-border);
        border-radius: var(--radius);
        margin-bottom: var(--sp-2);
        padding: var(--sp-2);
        background: var(--luna-surface);
    }
    table.data td { border: none; padding: var(--sp-1) var(--sp-2); }
    table.data td::before {
        content: attr(data-label);
        display: block;
        font-size: var(--fs-xs);
        text-transform: uppercase;
        letter-spacing: .4px;
        color: var(--luna-text-faint);
    }
    table.data td.numeric { text-align: left; }
    .table-wrap { border: none; background: transparent; }

    .tiles { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
    .tile-wide { grid-column: span 2; }
    .bookmark-grid { grid-template-columns: 1fr; }

    /* Touch targets stay at or above 44px. */
    .btn, .btn-icon { min-height: 40px; }
    .btn-icon { width: 40px; }
    .rail-link, .filter-link, .recent-item { min-height: 44px; }
}

@media (max-width: 380px) {
    :root { --fs-3xl: 28px; --fs-2xl: 22px; }
    .main { padding: var(--sp-2); padding-bottom: 84px; }
    .tiles { grid-template-columns: 1fr 1fr; gap: var(--sp-2); }
    .tile { min-height: 108px; padding: var(--sp-3); }
    .tile-wide { grid-column: span 2; }
    .account-secret { min-width: 0; width: 100%; }
    .toolbar { gap: var(--sp-1); }
}

@media print {
    .rail, .topbar, .toasts, .toolbar { display: none; }
    .app { display: block; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
    .tile:hover { transform: none; }
}
