/* Lexicon web client. Two themes through custom properties; no framework. */

:root {
    --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --font-mono: ui-monospace, "Cascadia Mono", "Courier New", monospace;
    --radius: 6px;
    --gap: 8px;
    --touch: 40px;
    /* Control height on a pointer device; touch layouts override it. */
    --control: 27px;
    --splitter: 7px;
}

/* Light theme */
:root[data-theme="light"] {
    --window: #f4f4f4;
    --surface: #ffffff;
    --surface-alt: #ececec;
    --surface-raised: #ffffff;
    --text: #1c1c1c;
    --text-muted: #5a5a5a;
    --border: #c3c3c3;
    --border-strong: #9a9a9a;
    --accent: #2a82da;
    --accent-text: #ffffff;
    --selection: #2a82da;
    --selection-text: #ffffff;
    --danger: #b3261e;
    --code-bg: #f2f2f2;
    /* C++ tokens in the desktop highlighter's light colours. */
    --code-keyword: #00008b;
    --code-string: #a52a2a;
    --code-comment: #006400;
    --code-preprocessor: #8b008b;
    --link: #1a5fb4;
    --text-disabled: #6f6f6f;
    --surface-disabled: #e4e4e4;
    --badge: #e6e6e6;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.18);
}

/* Dark theme mirrors the desktop palette. */
:root[data-theme="dark"] {
    --window: #353535;
    --surface: #2b2b2b;
    --surface-alt: #3f3f3f;
    --surface-raised: #323232;
    --text: #e6e6e6;
    --text-muted: #9a9a9a;
    --border: #555555;
    --border-strong: #6f6f6f;
    --accent: #2a82da;
    --accent-text: #ffffff;
    --selection: #2a82da;
    --selection-text: #ffffff;
    --danger: #ff6b6b;
    --code-bg: #000000;
    --code-keyword: #569cd6;
    --code-string: #ce9178;
    --code-comment: #6a9955;
    --code-preprocessor: #c586c0;
    --link: #64b5ff;
    --text-disabled: #9d9d9d;
    --surface-disabled: #323232;
    --badge: #454545;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.55);
}

* { box-sizing: border-box; }

/* The layout rules below set display on elements that are also toggled with
   the hidden attribute, which would otherwise lose to them. */
[hidden] { display: none !important; }

html, body {
    height: 100%;
    margin: 0;
}

body {
    display: flex;
    flex-direction: column;
    font-family: var(--font-ui);
    font-size: 14px;
    color: var(--text);
    background: var(--window);
    padding-top: env(safe-area-inset-top, 0);
    padding-bottom: env(safe-area-inset-bottom, 0);
}

h1, h2, h3 { margin: 0 0 var(--gap); font-weight: 600; }
p { margin: 0 0 var(--gap); }
a { color: var(--link); }
.spacer { flex: 1 1 auto; }
.hint { color: var(--text-muted); font-size: 0.85em; margin: 2px 0 0; }
.numeric { text-align: right; font-variant-numeric: tabular-nums; }
.empty { color: var(--text-muted); text-align: center; padding: 16px; }
.fatal { color: var(--danger); padding: 16px; }

/* --- Controls --------------------------------------------------------- */
button,
input,
select,
textarea {
    font: inherit;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 3px 8px;
    /* Dense by default: this is a tool for long sessions with a database.
       The phone rules below raise every target back to --touch. */
    min-height: var(--control);
}

textarea { resize: vertical; }

input[type="checkbox"] {
    min-height: 0;
    width: 18px;
    height: 18px;
    accent-color: var(--accent);
}

button {
    background: var(--surface-alt);
    cursor: pointer;
    white-space: nowrap;
}

button:hover:not(:disabled) { border-color: var(--border-strong); }
/* Disabled has to stay readable: it says "not now", not "invisible". */
button:disabled,
input:disabled,
select:disabled,
textarea:disabled {
    color: var(--text-disabled);
    background: var(--surface-disabled);
    border-color: var(--border);
    cursor: not-allowed;
}

button.primary {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--accent-text);
    font-weight: 600;
}

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

/* --- Menu bar --------------------------------------------------------- */
.menu-bar {
    display: flex;
    align-items: center;
    gap: var(--gap);
    padding: 6px 10px;
    background: var(--surface-alt);
    border-bottom: 1px solid var(--border);
    flex-wrap: wrap;
}

.brand { font-weight: 700; letter-spacing: 0.02em; }
.menu-toggle { display: none; }
.menu-list { display: flex; gap: 2px; }
.menu { position: relative; }

.menu-trigger {
    background: transparent;
    border-color: transparent;
}

.menu.open > .menu-trigger {
    background: var(--surface);
    border-color: var(--border);
}

.menu-popup {
    display: none;
    position: absolute;
    top: calc(100% + 2px);
    left: 0;
    min-width: 220px;
    padding: 4px;
    background: var(--surface-raised);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    z-index: 40;
}

.menu.open > .menu-popup { display: block; }

.menu-item {
    display: block;
    width: 100%;
    text-align: left;
    background: transparent;
    border-color: transparent;
    min-height: var(--touch);
}

.menu-item:hover { background: var(--surface-alt); }
.menu-separator { border: 0; border-top: 1px solid var(--border); margin: 4px 2px; }
.session-label { color: var(--text-muted); font-size: 0.85em; }

/* --- Login ------------------------------------------------------------ */
.login-view {
    flex: 1 1 auto;
    display: grid;
    place-items: center;
    padding: 16px;
}

.login-card {
    width: min(420px, 100%);
    padding: 24px;
    background: var(--surface-raised);
    border: 1px solid var(--border);
    border-radius: calc(var(--radius) * 2);
    box-shadow: var(--shadow);
}

.login-card h1 { font-size: 1.6em; }
.login-actions { margin: 16px 0 8px; }
.login-actions button { width: 100%; min-height: var(--touch); }

/* --- Main view -------------------------------------------------------- */
.main-view {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-height: 0;
    padding: 8px 10px;
    gap: 6px;
}

.action-bar {
    display: flex;
    align-items: center;
    gap: var(--gap);
    flex-wrap: wrap;
}

.search-label { font-weight: 600; }
.search-input { flex: 1 1 320px; max-width: 600px; min-height: var(--control); }
.action-buttons { display: flex; gap: 6px; flex-wrap: wrap; }
.overflow-menu { position: relative; }
.overflow-trigger { min-width: var(--touch); min-height: var(--touch); font-size: 1.2em; }
.overflow-menu .menu-popup { right: 0; left: auto; }
.overflow-menu.open > .menu-popup { display: block; }

.table-wrapper {
    flex: 1 1 45%;
    min-height: 180px;
    overflow: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.item-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
}

.item-table th,
.item-table td {
    padding: 3px 8px;
    border-bottom: 1px solid var(--border);
    border-right: 1px solid var(--border);
    text-align: left;
    white-space: nowrap;
    max-width: 380px;
    overflow: hidden;
    text-overflow: ellipsis;
}

.item-table thead th {
    position: sticky;
    top: 0;
    z-index: 2;
    background: var(--surface-alt);
    cursor: pointer;
    user-select: none;
}

.item-table thead .filter-row td {
    position: sticky;
    top: 28px;
    z-index: 1;
    background: var(--surface-alt);
    padding: 2px 4px;
}

.column-filter { width: 100%; min-width: 96px; min-height: 24px; padding: 2px 6px; }
select.column-filter { min-width: 122px; }
.sort-indicator { color: var(--accent); }

.item-table tbody tr:nth-child(even) { background: var(--surface-alt); }
.item-table tbody tr:hover { background: color-mix(in srgb, var(--accent) 18%, transparent); }

.item-table tbody tr.selected,
.item-table tbody tr.selected:hover {
    background: var(--selection);
    color: var(--selection-text);
}

.item-table tbody tr {
    height: var(--item-row-height, 28px);
    cursor: default;
}

.pagination {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
}

.page-label { color: var(--text-muted); }
.page-size { min-width: 72px; }

/* --- Splitter ----------------------------------------------------------- */
.splitter {
    flex: 0 0 var(--splitter);
    height: var(--splitter);
    border-radius: 3px;
    background: var(--surface-alt);
    border: 1px solid var(--border);
    cursor: row-resize;
    touch-action: none;
}

.splitter:hover,
.splitter:focus-visible { background: var(--border-strong); }
body.resizing { cursor: row-resize; user-select: none; }

/* --- List view ---------------------------------------------------------- */
.list-bar {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
}

.sort-label { color: var(--text-muted); }
.sort-select { min-width: 130px; }
.sort-direction { min-width: 36px; }
.filter-toggle.active { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

.filter-panel {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 6px 10px;
    max-height: 45vh;
    overflow: auto;
    padding: 8px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.filter-field { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.filter-field > label { font-size: 0.85em; color: var(--text-muted); }
.filter-field > input,
.filter-field > select { width: 100%; min-height: var(--touch); }

.item-list {
    flex: 1 1 45%;
    min-height: 140px;
    overflow: auto;
    margin: 0;
    padding: 0;
    list-style: none;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.item-card {
    display: flex;
    align-items: stretch;
    gap: 6px;
    padding: 8px 6px 8px 10px;
    border-bottom: 1px solid var(--border);
    cursor: default;
}

.item-card.selected { background: var(--selection); color: var(--selection-text); }
.item-card.selected .card-meta,
.item-card.selected .badge { color: var(--selection-text); }
.item-card.selected .badge { background: rgba(255, 255, 255, 0.22); }
.card-main { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.card-title { font-weight: 600; overflow-wrap: anywhere; }
.card-meta { font-size: 0.85em; color: var(--text-muted); overflow-wrap: anywhere; }
.card-badges { display: flex; flex-wrap: wrap; gap: 4px; }

.badge {
    font-size: 0.78em;
    padding: 1px 6px;
    border-radius: 10px;
    background: var(--badge);
    white-space: nowrap;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
}

.badge.muted { color: var(--text-muted); }
.badge.pinned { background: var(--accent); color: var(--accent-text); }

.card-open {
    flex: 0 0 auto;
    align-self: center;
    min-width: var(--touch);
    min-height: var(--touch);
    font-size: 1.3em;
    line-height: 1;
    background: transparent;
    border-color: transparent;
}

.item-card.selected .card-open { color: var(--selection-text); }
.item-list .empty { padding: 20px; text-align: center; color: var(--text-muted); }

.preview-area {
    flex: 1 1 35%;
    min-height: 140px;
    display: flex;
    flex-direction: column;
    gap: var(--gap);
}

.content-preview {
    flex: 1 1 auto;
    min-height: 100px;
    overflow: auto;
    padding: 10px 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.links-preview {
    flex: 0 0 auto;
    max-height: 120px;
    overflow: auto;
    padding: 8px 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.links-line { margin: 0 0 4px; }
.item-link { cursor: pointer; }

/* --- Markdown preview ------------------------------------------------- */
.markdown-preview { line-height: 1.5; word-wrap: break-word; }
.markdown-preview > :first-child { margin-top: 0; }
.markdown-preview h1,
.markdown-preview h2,
.markdown-preview h3,
.markdown-preview h4 { margin: 14px 0 6px; line-height: 1.25; }
.markdown-preview h1 { font-size: 1.5em; }
.markdown-preview h2 { font-size: 1.3em; }
.markdown-preview h3 { font-size: 1.15em; }
.markdown-preview ul,
.markdown-preview ol { padding-left: 24px; }

.markdown-preview blockquote {
    margin: 10px 0;
    padding: 4px 12px;
    border-left: 3px solid var(--border-strong);
    color: var(--text-muted);
}

.markdown-preview code {
    font-family: var(--font-mono);
    background: var(--code-bg);
    border-radius: 3px;
    padding: 1px 4px;
}

.markdown-preview pre {
    background: var(--code-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 10px;
    overflow-x: auto;
}

.markdown-preview pre code { padding: 0; background: none; }
.token-keyword { color: var(--code-keyword); font-weight: 600; }
.token-string { color: var(--code-string); }
.token-comment { color: var(--code-comment); }
.token-preprocessor { color: var(--code-preprocessor); }
.markdown-preview img { max-width: 100%; height: auto; }

.markdown-preview table {
    border-collapse: collapse;
    margin: 10px 0;
    display: block;
    overflow-x: auto;
    max-width: 100%;
}

.markdown-preview th,
.markdown-preview td {
    border: 1px solid var(--border-strong);
    padding: 4px 8px;
    text-align: left;
}

.markdown-preview th { background: var(--surface-alt); font-weight: 600; }
.markdown-preview hr { border: 0; border-top: 1px solid var(--border); margin: 14px 0; }

/* --- Dialogs ---------------------------------------------------------- */
.dialog {
    width: min(560px, calc(100vw - 24px));
    max-width: calc(100vw - 24px);
    max-height: calc(100vh - 24px);
    padding: 0;
    color: var(--text);
    background: var(--surface-raised);
    border: 1px solid var(--border);
    border-radius: calc(var(--radius) * 1.5);
    box-shadow: var(--shadow);
}

.dialog-wide { width: min(1000px, calc(100vw - 24px)); }

/* The item editor keeps one position and one size whatever the tab holds,
   like the desktop dialog. A centred dialog whose height follows its content
   moves its title and buttons every time a tab is switched. */
.dialog-item-editor {
    margin-top: 4vh;
    margin-bottom: auto;
    height: min(780px, 92vh);
}

.dialog-item-editor .dialog-form { height: 100%; max-height: none; }
/* The tab strip stays put; only the active tab scrolls. */
.dialog-item-editor .dialog-body { display: flex; overflow: hidden; }
/* min-width: 0, or the flex item grows to the width of the widest thing in it
   - the six tabs in a row, a long code line - and a phone clips the rest. */
.dialog-item-editor .tabs { flex: 1 1 auto; min-height: 0; min-width: 0; }
/* The strip keeps its full height; flex would otherwise squeeze it. */
.dialog-item-editor .tab-list { flex: 0 0 auto; }
.dialog-item-editor .tab-panels { flex: 1 1 auto; min-height: 0; overflow: auto; }
/* The Markdown editor and preview use the height the frame gives them. */
.dialog-item-editor #panel-content:not([hidden]) { height: 100%; }
.dialog-item-editor .content-panel { display: flex; flex-direction: column; height: 100%; }
.dialog-item-editor .content-split { flex: 1 1 auto; min-height: 220px; }
.dialog-item-editor .markdown-source { height: 100%; min-height: 0; resize: none; }
.dialog::backdrop { background: rgba(0, 0, 0, 0.5); }

.dialog-form {
    display: flex;
    flex-direction: column;
    max-height: calc(100vh - 28px);
    padding: 16px;
    gap: var(--gap);
}

.dialog-title { font-size: 1.15em; margin: 0; }
.dialog-body { flex: 1 1 auto; overflow: auto; min-height: 0; }
.dialog-error { color: var(--danger); margin: 0; }
.confirm { white-space: pre-wrap; }
.confirm.danger { border-left: 3px solid var(--danger); padding-left: 10px; }
.import-warnings { max-height: 40vh; overflow: auto; margin: 0; padding-left: 20px; }

.dialog-actions {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
    border-top: 1px solid var(--border);
    padding-top: var(--gap);
}

.dialog-actions button { min-height: var(--touch); min-width: 88px; }
.scroll-area { max-height: 60vh; overflow: auto; }

/* --- Forms ------------------------------------------------------------ */
.form-row { margin-bottom: 10px; }

.form-row > label {
    display: block;
    margin-bottom: 3px;
    font-weight: 600;
}

.form-row > input,
.form-row > select,
.form-row > textarea { width: 100%; }
.form-row > input[type="checkbox"] { width: 18px; }
.form-grid { display: grid; grid-template-columns: 1fr; gap: 2px; }
.item-date { position: relative; display: flex; gap: 5px; align-items: center; }
.item-date input[type="text"] { flex: 1 1 auto; min-width: 0; }
.item-date button { flex: none; }
.item-date-picker { position: absolute; right: 0; bottom: 0; width: 1px; height: 1px;
    padding: 0; border: 0; opacity: 0; pointer-events: none; }
.foreign-key-control input { width: 100%; }
.foreign-key-results { max-height: 180px; overflow-y: auto; border: 1px solid var(--border); background: var(--surface); }
.foreign-key-option { display: block; width: 100%; text-align: left; padding: 6px; }

/* --- Tabs ------------------------------------------------------------- */
.tabs { display: flex; flex-direction: column; min-height: 0; }

.tab-list {
    display: flex;
    gap: 2px;
    overflow-x: auto;
    border-bottom: 1px solid var(--border);
}

.tab {
    background: transparent;
    border: 1px solid transparent;
    border-bottom: none;
    border-radius: var(--radius) var(--radius) 0 0;
    min-height: var(--touch);
}

.tab.active {
    background: var(--surface);
    border-color: var(--border);
    font-weight: 600;
}

.tab-panels { padding-top: var(--gap); min-height: 0; }
.tab-panel { min-height: 0; }

/* --- List editors ----------------------------------------------------- */
.list-editor { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.list-editor h3 { font-size: 1em; margin: 0; }

.value-list {
    list-style: none;
    margin: 0;
    padding: 0;
    min-height: 120px;
    max-height: 240px;
    overflow: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.value-list li {
    padding: 6px 8px;
    cursor: default;
    border-bottom: 1px solid var(--border);
    overflow-wrap: anywhere;
}

.value-list li.selected { background: var(--selection); color: var(--selection-text); }
.list-editor-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.metadata-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
/* Tags and Flags share a row; Aliases and Properties span both, as in Qt. */
.metadata-grid > :nth-child(3),
.metadata-grid > :nth-child(4) { grid-column: 1 / -1; }
.type-manager { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }

/* --- Item editor ------------------------------------------------------ */
.markdown-toolbar {
    display: flex;
    gap: 4px;
    flex-wrap: wrap;
    margin-bottom: 6px;
}

.toolbar-button { min-width: 40px; min-height: 34px; }
/* Source and preview side by side need no switch; see the narrow layout. */
.content-mode { display: none; }
.toolbar-separator { width: 1px; background: var(--border); margin: 2px 4px; }

.content-split {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
    min-height: 300px;
}

.markdown-source {
    width: 100%;
    min-height: 300px;
    font-family: var(--font-mono);
    resize: vertical;
}

.board-list {
    width: 100%;
    min-height: 9rem;
}

.content-split .markdown-preview {
    overflow: auto;
    padding: 8px 10px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.values-panel { min-height: 120px; }
.blob-field { display: flex; flex-direction: column; gap: 6px; }
.blob-row { display: flex; gap: 6px; flex-wrap: wrap; }
.blob-hash { flex: 1 1 240px; font-family: var(--font-mono); font-size: 0.85em; }

/* --- Value tables ----------------------------------------------------- */
.value-table { width: 100%; border-collapse: collapse; }

.value-table th,
.value-table td {
    border: 1px solid var(--border);
    padding: 5px 8px;
    text-align: left;
}

.value-table th { background: var(--surface-alt); position: sticky; top: 0; }

/* --- Images ----------------------------------------------------------- */
.image-field { display: flex; gap: 10px; align-items: flex-start; flex-wrap: wrap; }
.image-side { display: flex; flex-direction: column; gap: 6px; flex: 1 1 220px; }
.image-thumbnail {
    width: 160px; height: 120px; padding: 0; display: flex; align-items: center; justify-content: center;
    background: var(--surface-alt); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden;
}
.image-thumbnail:disabled { cursor: default; }
.image-thumbnail img { max-width: 100%; max-height: 100%; object-fit: contain; }
.item-images { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--border); }
.item-image { margin: 0; display: flex; flex-direction: column; gap: 4px; max-width: 100%; }
.item-image figcaption { font-weight: 600; font-size: 0.9em; }
.image-preview {
    padding: 0; max-width: 100%; line-height: 0; border: 1px solid var(--border);
    background: var(--surface-alt); border-radius: var(--radius); overflow: hidden;
}
/* Fixed limits: a percentage of a shrink-to-fit button would be circular. */
.image-preview img { display: block; max-width: 320px; max-height: 240px; width: auto; height: auto; }
@media (max-width: 400px) { .image-preview img { max-width: 260px; } }
img.loading { min-width: 40px; min-height: 30px; }
img.broken { font-size: 0.85em; color: var(--text-muted); padding: 6px; }
.image-viewer { display: flex; flex-direction: column; gap: 8px; }
.image-frame { overflow: auto; max-height: 70vh; background: var(--surface-alt); border-radius: var(--radius); text-align: center; }
.image-frame.fit img { max-width: 100%; max-height: 68vh; object-fit: contain; }
.image-viewer-bar { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }

.alarm-table tbody tr[data-id] { cursor: default; }
.alarm-table tbody tr.past td { color: var(--text-muted); }
.alarm-table tbody tr.selected td { background: var(--selection); color: var(--selection-text); }
.alarm-table tbody tr:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.alarm-table .alarm-when { white-space: nowrap; font-variant-numeric: tabular-nums; }
.alarm-table .alarm-description { max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.alarms { display: flex; flex-direction: column; gap: 6px; }
.alarm-summary { margin: 0; }
.alarm-table tbody tr.ringing td { font-weight: 600; }
.alarm-notifications { margin: 4px 0 0; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* The bell: alarms that have gone off, over the page until dealt with. */
.alarm-bell {
    position: fixed; z-index: 90; right: 12px; top: calc(52px + env(safe-area-inset-top, 0px));
    width: min(380px, calc(100vw - 24px)); max-height: 70vh; overflow: auto;
    display: flex; flex-direction: column; gap: 8px; padding: 10px 12px;
    background: var(--surface-raised); color: var(--text);
    border: 2px solid var(--accent); border-radius: var(--radius); box-shadow: var(--shadow);
}
.alarm-bell-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.alarm-bell-list { display: flex; flex-direction: column; gap: 8px; }
.alarm-card { display: flex; flex-direction: column; gap: 3px; padding: 8px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
.alarm-card-description { margin: 2px 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.alarm-card-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 4px; }

/* --- Responsive ------------------------------------------------------- */
@media (max-width: 860px) {
    .metadata-grid,
    .type-manager,
    .content-split { grid-template-columns: minmax(0, 1fr); }

    .dialog,
    .dialog-wide,
    .dialog-item-editor {
        width: 100vw;
        max-width: 100vw;
        height: 100%;
        max-height: 100%;
        margin: 0;
        border-radius: 0;
        border: 0;
    }

    .dialog-form { max-height: 100vh; }
    .markdown-source, .content-split { min-height: 200px; }

    /* One column: source and preview take turns at the full height. */
    .content-mode { display: grid; grid-template-columns: 1fr 1fr; margin-bottom: 6px; }
    .content-mode-button { min-height: var(--touch); border-radius: 0; }
    .content-mode-button:first-child { border-radius: var(--radius) 0 0 var(--radius); }
    .content-mode-button:last-child { border-radius: 0 var(--radius) var(--radius) 0; }
    .content-mode-button.active {
        background: var(--accent);
        border-color: var(--accent);
        color: var(--accent-text);
    }
    .content-panel:not(.previewing) .content-split .markdown-preview { display: none; }
    .content-panel.previewing .markdown-toolbar,
    .content-panel.previewing .markdown-source { display: none; }
}

@media (max-width: 720px) {
    /* Touch first: every target goes back to a finger-sized minimum. */
    :root { --control: var(--touch); }

    /* iOS zooms the page into any field whose text is smaller than 16px, and
       does not zoom back out. At 16px it leaves the page alone. */
    input, select, textarea, .blob-hash { font-size: 16px; }

    .menu-toggle { display: inline-block; min-height: var(--touch); min-width: var(--touch); }
    .menu-bar { padding: 4px 8px; gap: 6px; }

    /* The menu is a drawer over the page, not a block that pushes it down. */
    .menu-list {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        width: min(300px, 82vw);
        flex-direction: column;
        gap: 0;
        padding: 8px;
        padding-top: calc(8px + env(safe-area-inset-top, 0px));
        overflow-y: auto;
        background: var(--surface-raised);
        border-right: 1px solid var(--border);
        box-shadow: var(--shadow);
        z-index: 60;
    }

    .menu-bar.menu-open .menu-list { display: flex; }
    /* A backdrop so a tap anywhere else closes the drawer. */
    .menu-bar.menu-open::before {
        content: "";
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.45);
        z-index: 50;
    }

    .menu-bar.menu-open .menu-trigger { width: 100%; text-align: left; font-weight: 600; }
    .menu-bar.menu-open .menu-popup {
        position: static;
        display: block;
        min-width: 0;
        padding: 0 0 4px 10px;
        background: transparent;
        border: 0;
        box-shadow: none;
    }

    .session-label { width: 100%; order: 10; font-size: 0.8em; }

    .main-view { padding: 6px 8px; gap: 5px; }
    .search-label { display: none; }
    .search-input { flex: 1 1 auto; max-width: none; }
    /* Search and quick add stay on one line; the rest is behind the menu. */
    .action-bar { flex-wrap: nowrap; }
    .action-buttons { display: contents; }
    .action-bar .action-buttons > button { flex: 0 0 auto; min-width: var(--touch); }

    .list-bar { gap: 4px; }
    .sort-select { flex: 1 1 auto; min-width: 0; }
    .filter-panel { grid-template-columns: minmax(0, 1fr); }

    .table-wrapper, .item-list { flex: 1 1 auto; min-height: 200px; -webkit-overflow-scrolling: touch; }
    .item-table th, .item-table td { max-width: 220px; }

    /* Nothing selected means nothing to preview; the list gets the room. */
    .preview-area { flex: 0 1 auto; }
    .preview-area.no-selection { display: none; }
    .content-preview { max-height: 34vh; }
    .splitter { display: none; }

    /* Page size keeps its label instead of dropping onto its own line. */
    .pagination { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; }
    .pagination button { min-height: var(--touch); }
    .pagination .spacer { display: none; }
    /* The label first, then one row of four buttons. */
    .page-label { grid-column: 1 / -1; order: -1; text-align: center; }
    .page-size-label { grid-column: 1 / 3; align-self: center; text-align: right; }
    .page-size { grid-column: 3 / -1; }
}

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

/* Review */
.review-header { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.review-card { display: flex; flex-direction: column; gap: 8px; }
.review-title { font-size: 1.6em; margin: 8px 0 0; overflow-wrap: anywhere; }
.review-details { margin: 0; }
.review-content { max-height: 50vh; overflow: auto; }
.review-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.review-done { text-align: center; margin: 24px 0; }

/* Wiki links and the Links tab */
.links-panel { display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.links-panel > .list-editor { align-self: stretch; }
a.wiki-link { text-decoration-style: dotted; }

/* Relationship graph */
.graph-header { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.graph-summary-slot { flex: 1 1 auto; }
.graph-tools { display: flex; gap: 4px; flex-wrap: wrap; }
.graph-tool { min-width: 36px; }
.graph-tool[aria-pressed="true"] { border-color: var(--accent); }
.graph-canvas { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface);
    height: min(62vh, 640px); overflow: auto; display: flex; }
.graph-canvas.panning { cursor: grabbing; user-select: none; }
/* margin: auto centres the drawing and, unlike justify-content, lets every
   part of a zoomed one be scrolled to. */
svg.graph { flex: none; margin: auto; }
/* Full screen: the graph takes the whole window, the canvas all it can. */
.dialog.graph-full-screen { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh;
    margin: 0; border: 0; border-radius: 0; }
.graph-full-screen .dialog-form { height: 100%; max-height: none; }
.graph-full-screen .dialog-body { display: flex; }
.graph-full-screen .graph-dialog { flex: 1 1 auto; display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.graph-full-screen .graph-canvas { flex: 1 1 auto; height: auto; min-height: 0; }
.graph-edge { stroke: var(--border-strong); stroke-width: 1.5; }
.graph-arrow { fill: var(--border-strong); }
.graph-edge-label { fill: var(--text-muted); font-size: 11px; }
.graph-node { cursor: pointer; }
.graph-node circle { fill: var(--surface-alt); stroke: var(--border-strong); stroke-width: 1.5; }
.graph-node.centre circle { fill: var(--accent); stroke: var(--accent); stroke-width: 3; }
.graph-node:focus-visible circle, .graph-node:hover circle { stroke: var(--accent); stroke-width: 3; }
.graph-node:focus { outline: none; }
.graph-caption { fill: var(--text); font-size: 13px; }

/* Why a search found an item: a quiet line under its title */
.match-snippet {
    display: block;
    margin-top: 2px;
    max-width: 46ch;
    color: var(--text-muted);
    font-size: 0.82em;
    font-weight: normal;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.item-card .match-snippet { max-width: none; white-space: normal; }

/* Inbox */
.inbox-content { width: 100%; font-family: var(--font-ui); resize: vertical; }

/* Mass Insert: a deliberately wide worksheet kept inside the dialog. */
.dialog-mass-insert { width: min(98vw, 1600px); max-width: 98vw; }
.mass-insert { min-width: 0; }
.mass-insert-scroll { max-height: 65vh; overflow: auto; border: 1px solid var(--border); }
.mass-insert-table { width: max-content; min-width: 100%; margin: 0; }
.mass-insert-table thead { position: sticky; top: 0; z-index: 1; background: var(--surface-alt); }
.mass-insert-table th:first-child, .mass-insert-table td:first-child { position: sticky; left: 0; z-index: 1; background: var(--surface); }
.mass-insert-table thead th:first-child { z-index: 2; background: var(--surface-alt); }
.mass-insert-table input[type="text"], .mass-insert-table input[type="number"],
.mass-insert-table input[type="search"], .mass-insert-table input[type="time"],
.mass-insert-table input[type="datetime-local"], .mass-insert-table select,
.mass-insert-table textarea { min-width: 150px; width: 100%; }
.mass-insert-table textarea { min-height: 3.2rem; resize: both; }
.mass-insert-table td.mass-insert-check { text-align: center; }
.mass-insert-table td.mass-insert-check input { min-width: auto; width: auto; }
.mass-insert-date { position: relative; display: flex; gap: 5px; align-items: center; }
.mass-insert-date input[type="text"] { min-width: 120px; }
.mass-insert-date button { flex: none; }
.mass-insert-date-picker { position: absolute; right: 0; bottom: 0; width: 1px; height: 1px;
    padding: 0; border: 0; opacity: 0; pointer-events: none; }
.mass-insert-image { min-width: 190px; display: grid; gap: 5px; }
.mass-insert-image-buttons { display: flex; gap: 5px; }
.mass-insert-image-buttons button { white-space: nowrap; }
.mass-insert-tools { display: flex; align-items: center; gap: 12px; margin-top: 8px; }
.mass-insert-backup { margin-left: auto; }

/* Cards: plain text with its line breaks, never markup */
.cards { display: flex; flex-direction: column; gap: 6px; }
.cards-item-title { margin: 0; font-size: 1.2em; overflow-wrap: anywhere; }
.card-table .card-text { white-space: pre-wrap; overflow-wrap: anywhere; max-width: 360px; vertical-align: top; }
.card-table .card-count { text-align: right; font-variant-numeric: tabular-nums; vertical-align: top; }
.card-table .card-when { white-space: nowrap; font-variant-numeric: tabular-nums; vertical-align: top; }
.card-table tbody tr[data-id] { cursor: default; }
.card-table tbody tr.selected td { background: var(--selection); color: var(--selection-text); }
.card-table tbody tr:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.card-edit .card-field { width: 100%; resize: vertical; font-family: var(--font-ui); }
.card-statistics { margin-top: 8px; }

/* Card quiz */
.quiz { display: flex; flex-direction: column; gap: 8px; }
.quiz-scope { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.quiz-scope label { display: inline-flex; align-items: center; gap: 4px; }
.quiz-truncated { margin: 0; }
.quiz-card { display: flex; flex-direction: column; gap: 6px; }
.quiz-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.quiz-source { margin: 0; overflow-wrap: anywhere; color: var(--text-muted); }
.quiz-progress { white-space: nowrap; font-variant-numeric: tabular-nums; }
.quiz-heading { margin: 6px 0 0; font-size: 1em; padding-bottom: 2px; border-bottom: 1px solid var(--border); }
.quiz-text { margin: 0; font-size: 1.2em; white-space: pre-wrap; overflow-wrap: anywhere; max-height: 30vh; overflow: auto; }
.quiz-text:focus { outline: none; }
.quiz-answer-box { display: flex; flex-direction: column; gap: 6px; }
.quiz-verdict { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 4px; }
.quiz-verdict button { min-width: 80px; }
.quiz-end { text-align: center; }
.quiz-done { margin: 24px 0 12px; white-space: pre-wrap; font-size: 1.15em; }

@media (max-width: 720px) {
    /* A card per block: the text over the whole width, the counts under it. */
    .card-table thead { display: none; }
    .card-table tbody tr[data-id] { display: grid; grid-template-columns: repeat(3, auto);
        padding: 6px 0; border-bottom: 1px solid var(--border); }
    .card-table tbody tr[data-id] td { border: 0; padding: 1px 6px; }
    .card-table td.card-text { grid-column: 1 / -1; max-width: none; }
    /* Without the column heads, the question stands out from its answer. */
    .card-table td.card-text:first-child { font-weight: 600; }
    .card-table td[data-label] { text-align: left; font-size: 0.85em; color: var(--text-muted); }
    .card-table tbody tr.selected td[data-label] { color: var(--selection-text); }
    .card-table td[data-label]::before { content: attr(data-label) ": "; }
    .quiz-verdict button { flex: 1 1 0; min-height: var(--touch); }
    .quiz-show { min-height: var(--touch); }
}
.history-entries { width: 100%; min-height: 12rem; }
.history-preview { max-height: 18rem; overflow: auto; white-space: pre-wrap; }
.session-row { display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: .5rem 0; }

/* Study Plan dashboard */
.study-plans { max-height: min(72vh, 760px); overflow-y: auto; }
.study-plans section { margin-top: 1.3rem; }
.study-plans section h2 { margin-bottom: .55rem; }
.study-browse { margin-top: 1.3rem; border-top: 1px solid var(--border); padding-top: .7rem; }
.study-browse summary { cursor: pointer; font-size: 1.2rem; font-weight: 600; }
.study-browse-body { margin-top: .7rem; }
.study-browse input[type="search"] { width: 100%; box-sizing: border-box; }
.study-pagination { display: flex; flex-wrap: wrap; align-items: center; gap: .6rem; margin: .7rem 0; }
.study-pagination span { flex: 1; text-align: center; }
.study-card { border: 1px solid var(--border); border-radius: 8px; padding: .8rem 1rem; margin: .65rem 0; }
.study-card h3 { margin: 0 0 .3rem; }
.study-card p { margin: .35rem 0; }
.study-card-warning { border-left: 5px solid #b3261e; }
.study-warning { color: #b3261e; font-weight: 700; }
.study-status { font-weight: 600; }
.study-note { white-space: pre-wrap; }
.study-actions { display: flex; flex-wrap: wrap; gap: .4rem; margin-top: .7rem; }
.study-weekdays { display: flex; flex-wrap: wrap; gap: .7rem; margin: .8rem 0; }
.study-day { display: inline-flex; gap: .3rem; align-items: center; }
@media (max-width: 600px) {
    .study-plans { max-height: 70vh; }
    .study-actions button { flex: 1 1 45%; }
}
