:root {
    --bg: #eef0f4;
    --panel: #ffffff;
    --border: #d7dae1;
    --text: #20242e;
    --muted: #6b7280;
    --accent: #2f6fed;
    --danger: #d64545;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #14161b;
        --panel: #1e2229;
        --border: #343b47;
        --text: #e7eaf0;
        --muted: #98a1b0;
    }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
    margin: 0;
    display: flex;
    flex-direction: column;
    font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
    background: var(--bg);
    color: var(--text);
}

button, select, input {
    font: inherit;
    color: inherit;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.3rem 0.6rem;
}
button { cursor: pointer; }
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }

.sep { width: 1px; align-self: stretch; background: var(--border); margin: 0 0.35rem; }
.grow { flex: 1; }
.error { color: var(--danger); }
.brand { margin-right: 0.5rem; }
#page-input { width: 4.5em; text-align: center; }

#topbar, #toolbar, #statusbar {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.45rem 0.75rem;
    background: var(--panel);
}
#topbar { border-bottom: 1px solid var(--border); }
#toolbar { border-top: 1px solid var(--border); overflow-x: auto; }
#statusbar { border-top: 1px solid var(--border); font-size: 12px; color: var(--muted); gap: 1rem; }

#layout { flex: 1; display: flex; min-height: 0; }
#thumbs {
    width: 170px;
    overflow-y: auto;
    padding: 0.6rem;
    background: var(--panel);
    border-right: 1px solid var(--border);
    position: relative;
}
#thumbs-toolbar {
    position: sticky;
    top: -0.6rem;
    z-index: 3;
    display: flex;
    flex-wrap: wrap;
    gap: 3px;
    padding: 0.35rem 0;
    margin: -0.6rem 0 0.5rem;
    background: var(--panel);
    border-bottom: 1px solid var(--border);
}
#thumbs-toolbar button { padding: 2px 6px; font-size: 12px; }
#viewer { flex: 1; overflow: auto; position: relative; padding: 8px 0; }

.page {
    position: relative;
    margin: 16px auto;
    background: #fff;
    box-shadow: 0 1px 5px rgba(0, 0, 0, 0.3);
}
.page canvas { display: block; width: 100%; height: 100%; }
.overlay-slot { position: absolute; inset: 0; }

.thumb {
    border: 2px solid transparent;
    border-radius: 5px;
    padding: 2px;
    cursor: pointer;
    margin-bottom: 0.5rem;
}
.thumb.active { border-color: var(--accent); }
.thumb.selected { background: color-mix(in srgb, var(--accent) 18%, transparent); border-color: var(--accent); }
.thumb.dragging { opacity: 0.4; }
.thumb canvas { display: block; width: 100%; background: #fff; pointer-events: none; }
.thumb .num { text-align: center; font-size: 11px; color: var(--muted); margin-top: 2px; }

.drop-marker {
    position: absolute;
    left: 0.4rem;
    right: 0.4rem;
    height: 3px;
    border-radius: 2px;
    background: var(--accent);
    pointer-events: none;
    z-index: 2;
}

.context-menu {
    position: fixed;
    z-index: 60;
    min-width: 200px;
    padding: 4px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 6px;
    box-shadow: 0 4px 18px rgba(0, 0, 0, 0.25);
    display: flex;
    flex-direction: column;
}
.context-menu button {
    text-align: left;
    border: 0;
    background: none;
    padding: 6px 10px;
    border-radius: 4px;
    font-size: 13px;
}
.context-menu button:hover { background: color-mix(in srgb, var(--accent) 15%, transparent); }
.context-menu .menu-sep { height: 1px; margin: 4px 6px; background: var(--border); }

.page-dialog {
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--panel);
    color: inherit;
    padding: 1rem 1.2rem;
    min-width: 300px;
}
.page-dialog::backdrop { background: rgba(0, 0, 0, 0.4); }
.page-dialog h4 { margin: 0 0 0.7rem; }
.page-dialog label { display: block; margin: 0.5rem 0; font-size: 13px; }
.page-dialog input, .page-dialog select { width: 100%; margin-top: 3px; }
.page-dialog .dialog-actions { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 1rem; }
.page-dialog .dialog-error { color: #d33; font-size: 12px; }
.page-dialog .muted { color: var(--muted); font-size: 12px; margin: 0.2rem 0; }

.dropzone {
    max-width: 420px;
    margin: 12vh auto;
    padding: 2.5rem 2rem;
    text-align: center;
    border: 2px dashed var(--border);
    border-radius: 12px;
    background: var(--panel);
    display: flex;
    flex-direction: column;
    gap: 0.9rem;
    align-items: center;
}
body.dragging .dropzone { border-color: var(--accent); }

#progress {
    width: 100%;
    height: 8px;
    border-radius: 4px;
    background: var(--border);
    overflow: hidden;
}
#progress > div {
    height: 100%;
    width: 0;
    background: var(--accent);
    transition: width 0.15s ease;
}

#banner {
    padding: 0.5rem 0.75rem;
    background: #8a5a00;
    color: #fff;
    font-size: 13px;
    text-align: center;
}

#inspector {
    width: 280px;
    background: var(--panel);
    border-left: 1px solid var(--border);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}
#inspector.collapsed { width: auto; }
#inspector.collapsed #inspector-body { display: none; }
#inspector-toggle { margin: 0.5rem; align-self: flex-start; }
#inspector-body { padding: 0 0.75rem 0.75rem; font-size: 13px; }
#inspector h4 {
    margin: 0.8rem 0 0.4rem;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--muted);
}
.muted { color: var(--muted); }

.font-row { padding: 0.35rem 0; border-bottom: 1px solid var(--border); }
.font-row .badges { display: flex; gap: 0.3rem; flex-wrap: wrap; margin-top: 3px; }
.badge {
    font-size: 10px;
    padding: 1px 6px;
    border-radius: 8px;
    border: 1px solid var(--border);
    color: var(--muted);
    white-space: nowrap;
}
.badge.on { border-color: var(--accent); color: var(--accent); }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 3px 10px; margin: 0; }
.kv dt { color: var(--muted); white-space: nowrap; }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.swatch {
    display: inline-block;
    width: 12px;
    height: 12px;
    border-radius: 3px;
    border: 1px solid var(--border);
    vertical-align: -2px;
    margin-right: 4px;
}

.overlay-slot { pointer-events: none; }
.overlay-slot .canvas-container {
    position: absolute !important;
    inset: 0;
    pointer-events: auto;
}

#toolbar .tool.active {
    border-color: var(--accent);
    color: var(--accent);
}

.edit-badge {
    position: absolute;
    z-index: 5;
    font-size: 10px;
    line-height: 1;
    padding: 3px 6px;
    border-radius: 8px;
    background: var(--panel);
    border: 1px solid var(--border);
    color: var(--muted);
    pointer-events: none;
    white-space: nowrap;
}
.edit-badge.tier-1 { border-color: #2e9e5b; color: #2e9e5b; }
.edit-badge.tier-2 { border-color: var(--accent); color: var(--accent); }
.edit-badge.tier-3 { border-color: #b8860b; color: #b8860b; }

#floating-toolbar, .floating-bar {
    position: fixed;
    z-index: 50;
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 5px 6px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 3px 12px rgba(0, 0, 0, 0.25);
}
#floating-toolbar select, #floating-toolbar input, #floating-toolbar button,
.floating-bar select, .floating-bar input, .floating-bar button {
    padding: 2px 6px;
    font-size: 12px;
}
#floating-toolbar input[type='number'], .floating-bar input[type='number'] { width: 4.2em; }
#floating-toolbar input[type='color'], .floating-bar input[type='color'] { width: 28px; height: 24px; padding: 1px; }
#floating-toolbar button.active, .floating-bar button.active { border-color: var(--accent); color: var(--accent); }
#floating-toolbar .toolbar-menu {
    position: absolute;
    top: calc(100% + 4px);
    right: 0;
    display: flex;
    flex-direction: column;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 6px;
    overflow: hidden;
}
#floating-toolbar .toolbar-menu button { border: 0; border-radius: 0; text-align: left; }

#context-menu {
    position: fixed;
    z-index: 60;
    display: flex;
    flex-direction: column;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 6px;
    overflow: hidden;
    box-shadow: 0 3px 12px rgba(0, 0, 0, 0.25);
}
#context-menu button { border: 0; border-radius: 0; text-align: left; }

.debug-box {
    position: absolute;
    border: 1px solid rgba(255, 0, 128, 0.9);
    background: rgba(255, 0, 128, 0.08);
    cursor: pointer;
    pointer-events: auto;
}
.debug-box:hover { background: rgba(255, 0, 128, 0.2); }

/* Prompt 7: Add text / Whiteout / Redact */
.floating-bar { flex-wrap: wrap; max-width: 640px; }
.floating-bar input[type='range'] { width: 70px; }
.floating-bar .bar-note {
    flex-basis: 100%;
    font-size: 11px;
    color: var(--muted);
}
.tier-note {
    font-size: 10px;
    line-height: 1;
    padding: 3px 6px;
    border-radius: 8px;
    border: 1px solid var(--border);
    color: var(--muted);
    white-space: nowrap;
}
.tier-note.tier-1 { border-color: #2e9e5b; color: #2e9e5b; }
.tier-note.tier-2 { border-color: var(--accent); color: var(--accent); }
.tier-note.tier-3 { border-color: #b8860b; color: #b8860b; }
.tier-note[hidden] { display: none; }

#btn-apply-redactions {
    border-color: #dc2626;
    color: #dc2626;
    pointer-events: auto;
}

.dialog-overlay {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.45);
}
.dialog {
    max-width: 420px;
    padding: 16px 18px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
}
.dialog h3 { margin: 0 0 8px; }
.dialog p { margin: 0 0 14px; font-size: 13px; line-height: 1.45; }
.dialog-buttons { display: flex; justify-content: flex-end; gap: 8px; }
.dialog-buttons .primary { border-color: #dc2626; color: #fff; background: #dc2626; }

/* ----- Prompt 8: images, drawing, annotations, signatures ----- */

/* Fixed options bars (Draw/Shapes and Highlight tools) dock above the toolbar. */
.options-bar {
    left: 50%;
    transform: translateX(-50%);
    bottom: 56px;
}

#export-annot-mode {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 12px;
    color: var(--muted, #9aa0a6);
    margin-right: 6px;
    white-space: nowrap;
}

.note-popover {
    flex-direction: column;
    align-items: stretch;
    max-width: 260px;
}
.note-popover textarea {
    width: 240px;
    resize: vertical;
    font: inherit;
    font-size: 12px;
}

/* Signature modal */
.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 100;
    background: rgba(0, 0, 0, 0.45);
    display: flex;
    align-items: center;
    justify-content: center;
}
.modal {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.4);
    padding: 14px;
    width: min(640px, 92vw);
    max-height: 88vh;
    overflow: auto;
}
.modal-head { display: flex; align-items: center; gap: 6px; margin-bottom: 8px; }
.modal-close { margin-left: auto; font-size: 16px; line-height: 1; }
.modal-tabs { display: flex; gap: 4px; margin-bottom: 10px; }
.modal-tabs button.active { border-color: var(--accent); color: var(--accent); }
.modal-pane { display: flex; flex-direction: column; gap: 8px; }
.modal-actions { display: flex; align-items: center; gap: 8px; }
.signature-canvas {
    width: 100%;
    height: 200px;
    background: #fff;
    border: 1px dashed var(--border);
    border-radius: 6px;
    touch-action: none;
    cursor: crosshair;
}
.signature-preview { max-width: 100%; max-height: 140px; background: repeating-conic-gradient(#ddd 0 25%, #fff 0 50%) 0 0/16px 16px; }
.signature-name { font-size: 16px; padding: 6px; }
.signature-font-list { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.signature-font { font-size: 26px; padding: 8px; background: #fff; color: #1a237e; border: 1px solid var(--border); border-radius: 6px; }
.signature-font.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.signature-saved { margin-top: 12px; display: flex; flex-direction: column; gap: 6px; }
.signature-row { display: flex; align-items: center; gap: 8px; }
.signature-row img { height: 34px; max-width: 180px; background: #fff; border: 1px solid var(--border); border-radius: 4px; }

/* Prompt 9: autosave status, settings menu, restore banner buttons */
#status-save { white-space: nowrap; }
#banner button { margin-left: 8px; padding: 2px 10px; font-size: 12px; cursor: pointer; border-radius: 4px; border: 1px solid rgba(255, 255, 255, 0.6); background: transparent; color: #fff; }
#banner button.primary { background: #fff; color: #8a5a00; font-weight: 600; }
#settings { position: relative; }
#settings summary { list-style: none; cursor: pointer; padding: 2px 8px; border-radius: 4px; user-select: none; }
#settings summary::-webkit-details-marker { display: none; }
#settings[open] summary, #settings summary:hover { background: var(--border); }
#settings .settings-menu { position: absolute; right: 0; top: 100%; z-index: 60; background: var(--panel); border: 1px solid var(--border); border-radius: 6px; padding: 6px; min-width: 160px; box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35); }
#settings .settings-menu button { width: 100%; text-align: left; }

/* --- Tools menu (Prompt 11) ------------------------------------------------ */
#tools-menu { position: relative; }
#tools-menu summary { list-style: none; cursor: pointer; padding: 2px 8px; border-radius: 4px; user-select: none; }
#tools-menu summary::-webkit-details-marker { display: none; }
#tools-menu[open] summary, #tools-menu summary:hover { background: var(--border); }
#tools-menu .tools-menu { position: absolute; left: 0; top: 100%; z-index: 60; background: var(--panel); border: 1px solid var(--border); border-radius: 6px; padding: 6px; min-width: 170px; box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35); display: flex; flex-direction: column; gap: 2px; }
#tools-menu .tools-menu button { width: 100%; text-align: left; }

.tool-dialog { min-width: 340px; max-width: 460px; }
.tool-dialog .indent { margin-left: 1.4rem; }
.tool-dialog .inline-num { width: 4.5em; display: inline-block; }
.tool-dialog .warn { color: #e0a020; }
.tool-dialog label input[type="radio"] { width: auto; margin-right: 4px; }
.tool-dialog .tool-file-list { margin: 0.4rem 0; padding-left: 1.2rem; max-height: 200px; overflow: auto; }
.tool-dialog .tool-file-list li { display: flex; align-items: center; justify-content: space-between; gap: 6px; padding: 2px 0; font-size: 13px; }
.tool-dialog .tool-file-list .file-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tool-dialog .tool-file-list .file-actions { display: flex; gap: 2px; flex: none; }
.tool-dialog .tool-file-list .file-actions button { padding: 0 6px; }

/* --- Prompt 12: search, UX polish, accessibility, responsive --------------- */

/* Visible focus outlines for keyboard users (mouse clicks stay clean). */
:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}

/* Search panel */
#search-panel {
    position: fixed;
    top: 52px;
    right: 18px;
    z-index: 70;
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 6px 8px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
}
#search-panel input[type='search'] { width: 180px; }
#search-panel .search-count {
    font-size: 12px;
    color: var(--muted);
    min-width: 84px;
    text-align: center;
    white-space: nowrap;
}
#search-panel button { padding: 2px 8px; }
#search-panel .search-toggle.active { border-color: var(--accent); color: var(--accent); }
#search-panel .search-close { border: 0; background: none; font-size: 15px; }

/* Match highlights on the pages */
.search-layer { position: absolute; inset: 0; pointer-events: none; }
.search-hit {
    position: absolute;
    background: rgba(255, 200, 0, 0.35);
    outline: 1px solid rgba(200, 150, 0, 0.7);
    border-radius: 2px;
    pointer-events: auto;
    cursor: pointer;
}
.search-hit.current {
    background: rgba(255, 120, 0, 0.45);
    outline: 2px solid #e66a00;
}

/* Loading skeleton while a page (re)renders */
.page.loading::before {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(100deg, transparent 30%, rgba(127, 127, 127, 0.14) 50%, transparent 70%);
    background-size: 200% 100%;
    animation: skeleton 1.2s ease-in-out infinite;
}
@media (prefers-color-scheme: dark) {
    .page.loading { background: #2a2f38; }
}
@keyframes skeleton {
    from { background-position: 120% 0; }
    to { background-position: -80% 0; }
}
@media (prefers-reduced-motion: reduce) {
    .page.loading::before { animation: none; }
}

/* Toast notifications */
#toasts {
    position: fixed;
    right: 16px;
    bottom: 52px;
    z-index: 120;
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-items: flex-end;
    pointer-events: none;
}
.toast {
    max-width: 340px;
    padding: 8px 12px;
    font-size: 13px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-left: 3px solid var(--accent);
    border-radius: 8px;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3);
    animation: toast-in 0.2s ease-out;
}
.toast-error { border-left-color: var(--danger); }
.toast.leaving { opacity: 0; transition: opacity 0.3s; }
@keyframes toast-in {
    from { transform: translateY(6px); opacity: 0; }
    to { transform: none; opacity: 1; }
}

/* Keyboard shortcut help dialog */
.help-dialog { max-width: 460px; max-height: 80vh; overflow: auto; }
.help-dialog h5 {
    margin: 0.9rem 0 0.3rem;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
}
.shortcut-list {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 4px 14px;
    margin: 0;
    font-size: 13px;
}
.shortcut-list dt { white-space: nowrap; }
.shortcut-list dd { margin: 0; color: var(--muted); }
kbd {
    font: 11px/1.4 ui-monospace, monospace;
    padding: 1px 6px;
    border: 1px solid var(--border);
    border-bottom-width: 2px;
    border-radius: 4px;
    background: var(--bg);
}

/* Context-menu shortcut hints */
.context-menu button { display: flex; justify-content: space-between; gap: 18px; }
.context-menu .menu-hint { color: var(--muted); font-size: 11px; }

/* "More" tools menu (phones) */
#toolbar-more { position: relative; }
#toolbar-more summary {
    list-style: none;
    cursor: pointer;
    padding: 0.3rem 0.6rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    user-select: none;
    white-space: nowrap;
}
#toolbar-more summary::-webkit-details-marker { display: none; }
#toolbar-more[open] summary { border-color: var(--accent); color: var(--accent); }
#toolbar-more .toolbar-more-menu {
    position: absolute;
    bottom: calc(100% + 6px);
    right: 0;
    z-index: 70;
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 6px;
    min-width: 140px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3);
}
#toolbar-more .toolbar-more-menu .tool { text-align: left; }

/* Panel toggle button: desktop keeps the panels inline, so hide it there. */
#btn-toggle-thumbs { display: none; }

/* Tablet: collapsible side panels, horizontally scrolling tool bar. */
@media (max-width: 1024px) {
    #btn-toggle-thumbs { display: inline-block; }
    #thumbs {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        z-index: 80;
        width: min(220px, 70vw);
        transform: translateX(-100%);
        transition: transform 0.2s ease;
        box-shadow: 4px 0 18px rgba(0, 0, 0, 0.25);
    }
    body.thumbs-open #thumbs { transform: none; }
    #inspector.collapsed { width: auto; }
    #inspector:not(.collapsed) {
        position: fixed;
        top: 0;
        right: 0;
        bottom: 0;
        z-index: 80;
        width: min(300px, 80vw);
        box-shadow: -4px 0 18px rgba(0, 0, 0, 0.25);
    }
    #toolbar { overflow-x: auto; scrollbar-width: thin; }
    #topbar { flex-wrap: nowrap; overflow-x: auto; }
}

/* Phones: view / navigate / upload / download / simple add text & sign; the
   advanced tools live in the "More" menu (moved there by ui/responsive.js). */
@media (max-width: 640px) {
    #topbar { gap: 0.25rem; padding: 0.4rem 0.5rem; }
    #export-annot-mode { display: none; }
    #btn-rotate-view, #btn-fullscreen, #zoom-select, #btn-help { display: none; }
    .brand { display: none; }
    #page-input { width: 3.4em; }
    #search-panel { left: 8px; right: 8px; flex-wrap: wrap; }
    #search-panel input[type='search'] { flex: 1; width: auto; min-width: 100px; }
    #statusbar { gap: 0.5rem; overflow-x: auto; white-space: nowrap; }
    .options-bar { max-width: 96vw; }
}
