:root,
:root[data-theme="dark"] {
    --bg-page: #1e1e2e;
    --bg-header: #181825;
    --bg-list: #181825;
    --bg-list-header: #11111b;
    --bg-status: #11111b;
    --bg-editor: #1e1e2e;
    --bg-modal: #313244;
    --bg-input: #1e1e2e;
    --bg-input-row: #1e1e2e;
    --bg-pinbtn: #45475a;
    --bg-pinbtn-hover: #585b70;
    --bg-pinbtn-action: #313244;
    --bg-noteitem-hover: #313244;
    --bg-noteitem-active: #45475a;
    --bg-btn-secondary: #45475a;
    --bg-btn-secondary-hover: #585b70;
    --bg-cog-hover: #313244;
    --bg-demo-hint: #1e1e2e;

    --border: #313244;
    --border-input: #45475a;

    --text-primary: #cdd6f4;
    --text-muted: #a6adc8;
    --text-faint: #6c7086;
    --text-accent: #cba6f7;

    --btn-primary-bg: #89b4fa;
    --btn-primary-bg-hover: #74a0e8;
    --btn-primary-text: #1e1e2e;
    --btn-danger-bg: #f38ba8;
    --btn-danger-bg-hover: #e07696;
    --btn-danger-text: #1e1e2e;
    --btn-enter-bg: #a6e3a1;
    --btn-enter-bg-hover: #94d68a;
    --btn-enter-text: #1e1e2e;

    --status-saving: #f9e2af;
    --status-saved: #a6e3a1;
    --status-unsaved: #fab387;

    --dot-empty: #45475a;
    --dot-filled: #cba6f7;

    --demo-banner-bg: #f9e2af;
    --demo-banner-text: #1e1e2e;

    --msg-ok-bg: #a6e3a1;
    --msg-ok-text: #1e1e2e;
    --msg-err-bg: #f38ba8;
    --msg-err-text: #1e1e2e;

    --modal-overlay: rgba(0,0,0,0.7);
    --shadow-pin: 0 20px 60px rgba(0,0,0,0.4);
    --accent-tint: rgba(203,166,247,0.15);
}

/* Light theme: derived counterpart. Soft off-white surfaces, same purple accent
   so the brand feel stays consistent. Tested for AA contrast on text colours. */
:root[data-theme="light"] {
    --bg-page: #f5f5f7;
    --bg-header: #ffffff;
    --bg-list: #ffffff;
    --bg-list-header: #ececf0;
    --bg-status: #ececf0;
    --bg-editor: #fafafa;
    --bg-modal: #ffffff;
    --bg-input: #ffffff;
    --bg-input-row: #f5f5f7;
    --bg-pinbtn: #e4e4e9;
    --bg-pinbtn-hover: #d4d4dc;
    --bg-pinbtn-action: #ececf0;
    --bg-noteitem-hover: #ececf0;
    --bg-noteitem-active: #e0e0e8;
    --bg-btn-secondary: #e4e4e9;
    --bg-btn-secondary-hover: #d4d4dc;
    --bg-cog-hover: #ececf0;
    --bg-demo-hint: #fafafa;

    --border: #d8d8de;
    --border-input: #c8c8d0;

    --text-primary: #1e1e2e;
    --text-muted: #4a4a5e;
    --text-faint: #8a8a9a;
    --text-accent: #7c3aed;

    --btn-primary-bg: #4a7fdf;
    --btn-primary-bg-hover: #3a6dcd;
    --btn-primary-text: #ffffff;
    --btn-danger-bg: #d94865;
    --btn-danger-bg-hover: #c43855;
    --btn-danger-text: #ffffff;
    --btn-enter-bg: #4ca84c;
    --btn-enter-bg-hover: #3d903d;
    --btn-enter-text: #ffffff;

    --status-saving: #c08a00;
    --status-saved: #3d903d;
    --status-unsaved: #c46a1a;

    --dot-empty: #c8c8d0;
    --dot-filled: #7c3aed;

    --demo-banner-bg: #fff3cd;
    --demo-banner-text: #5a4a00;

    --msg-ok-bg: #d4edda;
    --msg-ok-text: #155724;
    --msg-err-bg: #f8d7da;
    --msg-err-text: #721c24;

    --modal-overlay: rgba(0,0,0,0.4);
    --shadow-pin: 0 20px 60px rgba(0,0,0,0.15);
    --accent-tint: rgba(124,58,237,0.12);
}

/* ============== BASE ============== */
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg-page);
    color: var(--text-primary);
    line-height: 1.5;
}

/* ============== PIN PAD ============== */
.pin-screen {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 20px;
}
.pin-box {
    background: var(--bg-modal);
    border-radius: 16px;
    padding: 40px;
    max-width: 360px;
    width: 100%;
    box-shadow: var(--shadow-pin);
}
.pin-title {
    text-align: center;
    margin-bottom: 8px;
    font-size: 24px;
    color: var(--text-accent);
}
.pin-subtitle {
    text-align: center;
    margin-bottom: 24px;
    font-size: 14px;
    color: var(--text-muted);
}
.pin-demo-hint {
    text-align: center;
    margin-bottom: 16px;
    padding: 10px;
    background: var(--bg-demo-hint);
    border: 1px dashed var(--border-input);
    border-radius: 8px;
    font-size: 12px;
    color: var(--text-muted);
    line-height: 1.4;
}
.pin-demo-hint strong { color: var(--text-accent); }
.pin-username {
    width: 100%;
    padding: 12px;
    margin-bottom: 16px;
    background: var(--bg-input);
    border: 2px solid var(--border-input);
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 16px;
    text-align: center;
}
.pin-username:focus {
    outline: none;
    border-color: var(--text-accent);
}
.pin-display {
    display: flex;
    justify-content: center;
    gap: 12px;
    margin-bottom: 24px;
}
.pin-dot {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--dot-empty);
    transition: background 0.2s;
}
.pin-dot.filled {
    background: var(--dot-filled);
}
.pin-pad {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
}
.pin-btn {
    aspect-ratio: 1;
    background: var(--bg-pinbtn);
    color: var(--text-primary);
    border: none;
    border-radius: 50%;
    font-size: 24px;
    cursor: pointer;
    transition: background 0.15s, transform 0.05s;
    font-weight: 500;
}
.pin-btn:hover { background: var(--bg-pinbtn-hover); }
.pin-btn:active { transform: scale(0.95); }
.pin-btn.action {
    font-size: 14px;
    background: var(--bg-pinbtn-action);
}
.pin-btn.enter { background: var(--btn-enter-bg); color: var(--btn-enter-text); }
.pin-btn.enter:hover { background: var(--btn-enter-bg-hover); }
.pin-error {
    text-align: center;
    color: var(--btn-danger-bg);
    margin-top: 16px;
    min-height: 20px;
    font-size: 14px;
}

/* ============== MAIN APP ============== */
.app {
    display: flex;
    flex-direction: column;
    height: 100vh;
}
.app-header {
    background: var(--bg-header);
    padding: 12px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}
.app-title { color: var(--text-accent); font-size: 18px; font-weight: 600; }
.app-header-right { display: flex; gap: 12px; align-items: center; }
.app-user { color: var(--text-muted); font-size: 14px; }
.demo-banner {
    background: var(--demo-banner-bg);
    color: var(--demo-banner-text);
    padding: 8px 16px;
    text-align: center;
    font-size: 13px;
    flex-shrink: 0;
    border-bottom: 1px solid var(--border);
}
.demo-banner strong { font-weight: 600; }
.demo-countdown { font-family: "SF Mono", Menlo, Monaco, Consolas, monospace; font-weight: 600; }
.btn {
    padding: 8px 16px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    transition: background 0.15s;
    font-family: inherit;
}
.btn-primary { background: var(--btn-primary-bg); color: var(--btn-primary-text); }
.btn-primary:hover { background: var(--btn-primary-bg-hover); }
.btn-danger { background: var(--btn-danger-bg); color: var(--btn-danger-text); }
.btn-danger:hover { background: var(--btn-danger-bg-hover); }
.btn-secondary { background: var(--bg-btn-secondary); color: var(--text-primary); }
.btn-secondary:hover { background: var(--bg-btn-secondary-hover); }

/* Settings cog: icon-only button on the far right of the header.
   Slightly larger gear so it reads as a proper icon button rather than a labelled action. */
.btn-cog {
    background: transparent;
    color: var(--text-muted);
    padding: 6px 8px;
    font-size: 20px;
    line-height: 1;
    border-radius: 6px;
}
.btn-cog:hover { background: var(--bg-cog-hover); color: var(--text-accent); }

.app-body {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* Left column: note list */
.note-list {
    width: 280px;
    background: var(--bg-list);
    border-right: 1px solid var(--border);
    overflow-y: auto;
    flex-shrink: 0;
}
.note-list-header {
    padding: 12px 16px;
    background: var(--bg-list-header);
    border-bottom: 1px solid var(--border);
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-faint);
    position: sticky;
    top: 0;
}
.note-item {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
    transition: background 0.15s;
    display: flex;
    align-items: flex-start;
    gap: 8px;
}
.note-item:hover { background: var(--bg-noteitem-hover); }
.note-item.active { background: var(--bg-noteitem-active); border-left: 3px solid var(--text-accent); }
.note-item-main {
    flex: 1;
    min-width: 0;
}

.note-item-title {
    font-weight: 500;
    color: var(--text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    margin-bottom: 4px;
    cursor: pointer; /* single click opens, double click renames */
}
.note-item-title:hover { color: var(--text-accent); }
.note-item-title-input {
    width: 100%;
    background: var(--bg-input);
    border: 1px solid var(--text-accent);
    border-radius: 4px;
    color: var(--text-primary);
    font: inherit;
    font-weight: 500;
    padding: 2px 6px;
    margin-bottom: 4px;
    outline: none;
}
.note-item-date {
    font-size: 11px;
    color: var(--text-faint);
}
.note-item-edit {
    background: transparent;
    border: none;
    color: var(--text-faint);
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    font-size: 14px;
    line-height: 1;
    flex-shrink: 0;
    opacity: 0;
    transition: opacity 0.15s, color 0.15s, background 0.15s;
    align-self: flex-start;
    margin-top: 1px;
}
.note-item:hover .note-item-edit { opacity: 1; }
.note-item-edit:hover { color: var(--text-accent); background: var(--accent-tint); }
.note-list-empty {
    padding: 20px 16px;
    color: var(--text-faint);
    font-size: 14px;
    text-align: center;
}

/* Right column: editor */
.editor {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.editor-status {
    padding: 8px 20px;
    background: var(--bg-status);
    color: var(--text-faint);
    font-size: 12px;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.editor-status.saving { color: var(--status-saving); }
.editor-status.saved { color: var(--status-saved); }
.editor-status.unsaved { color: var(--status-unsaved); }
.editor-status-text {
    flex: 1;
}

/* Theme toggle button: lives in the editor-status bar on the right.
   Icon-only, transparent background, swaps icon based on current theme. */
.theme-toggle {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-muted);
    cursor: pointer;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 14px;
    line-height: 1;
    font-family: inherit;
    transition: background 0.15s, color 0.15s, border-color 0.15s;
    display: inline-flex;
    align-items: center;
    gap: 4px;
}
.theme-toggle:hover {
    background: var(--bg-cog-hover);
    color: var(--text-accent);
    border-color: var(--text-accent);
}
.theme-toggle .theme-toggle-icon {
    font-size: 13px;
}

.editor-textarea {
    flex: 1;
    padding: 20px;
    background: var(--bg-editor);
    color: var(--text-primary);
    border: none;
    resize: none;
    font-family: "SF Mono", Menlo, Monaco, Consolas, monospace;
    font-size: 15px;
    line-height: 1.6;
    outline: none;
}
.editor-footer {
    padding: 12px 20px;
    background: var(--bg-header);
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: flex-end;
}

/* ============== ADMIN PANEL ============== */
.admin-modal-bg {
    position: fixed;
    inset: 0;
    background: var(--modal-overlay);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 100;
}
.admin-modal-bg.show { display: flex; }
.admin-modal {
    background: var(--bg-modal);
    border-radius: 12px;
    padding: 30px;
    max-width: 600px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
}
.admin-modal h2 { color: var(--text-accent); margin-bottom: 20px; }
.admin-modal h3 { color: var(--text-primary); margin: 20px 0 10px 0; font-size: 16px; }
.admin-form { display: flex; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }
.admin-form input {
    flex: 1;
    min-width: 100px;
    padding: 8px 12px;
    background: var(--bg-input);
    border: 1px solid var(--border-input);
    border-radius: 6px;
    color: var(--text-primary);
    font-family: inherit;
}
.admin-user-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px;
    background: var(--bg-input-row);
    border-radius: 6px;
    margin-bottom: 8px;
    flex-wrap: wrap;
    gap: 8px;
}
.admin-user-name { font-weight: 500; }
.admin-user-meta { font-size: 12px; color: var(--text-faint); }
.admin-user-actions { display: flex; gap: 6px; }
.admin-msg {
    padding: 10px;
    border-radius: 6px;
    margin-bottom: 12px;
    font-size: 14px;
    display: none;
}
.admin-msg.show { display: block; }
.admin-msg.ok { background: var(--msg-ok-bg); color: var(--msg-ok-text); }
.admin-msg.err { background: var(--msg-err-bg); color: var(--msg-err-text); }

/* ============== SETTINGS MODAL ============== */
/* Reuses .admin-modal-bg / .admin-modal styling for visual consistency.
   The settings-specific bits below add the option-row layout for the dropdowns. */
.settings-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 0;
    border-bottom: 1px solid var(--border-input);
    gap: 12px;
    flex-wrap: wrap;
}
.settings-row:last-of-type { border-bottom: none; }
.settings-label { color: var(--text-primary); flex: 1; min-width: 180px; }
.settings-label-main { font-weight: 500; margin-bottom: 2px; }
.settings-label-hint { font-size: 12px; color: var(--text-faint); }
.settings-select {
    padding: 8px 12px;
    background: var(--bg-input);
    border: 1px solid var(--border-input);
    border-radius: 6px;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 14px;
    cursor: pointer;
    min-width: 120px;
}
.settings-select:focus {
    outline: none;
    border-color: var(--text-accent);
}
.settings-actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    margin-top: 20px;
}

/* Confirm dialog */
.confirm-bg {
    position: fixed;
    inset: 0;
    background: var(--modal-overlay);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 200;
}
.confirm-bg.show { display: flex; }
.confirm-box {
    background: var(--bg-modal);
    border-radius: 12px;
    padding: 24px;
    max-width: 400px;
    width: 90%;
    text-align: center;
}
.confirm-box p { margin-bottom: 20px; color: var(--text-primary); }
.confirm-actions { display: flex; gap: 8px; justify-content: center; }

/* ============== MOBILE LAYOUT ============== */
.notes-toggle {
    display: none; /* hidden on desktop */
    width: 100%;
    padding: 12px 16px;
    background: var(--bg-list);
    border: none;
    border-bottom: 1px solid var(--border);
    color: var(--text-primary);
    font-size: 14px;
    text-align: left;
    cursor: pointer;
    font-family: inherit;
}
.notes-toggle:hover { background: var(--bg-noteitem-hover); }
.notes-toggle .arrow {
    float: right;
    transition: transform 0.2s;
}
.notes-toggle.open .arrow { transform: rotate(180deg); }

/* Mobile-only icon button styling */
.btn-icon { display: none; }

@media (max-width: 700px) {
    /* Stack the body vertically */
    .app-body {
        flex-direction: column;
    }

    /* Compact header: shrink the user text, hide button labels, show icons */
    .app-header { padding: 10px 12px; }
    .app-title { font-size: 16px; }
    .app-user { font-size: 12px; max-width: 80px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .app-header-right { gap: 6px; }
    .btn-label { display: none; }
    .btn-icon {
        display: inline-block;
        font-size: 18px;
        line-height: 1;
    }
    .app-header-right .btn {
        padding: 8px 10px;
        min-width: 36px;
    }
    /* Cog stays icon-only on mobile too, just slightly tighter padding */
    .btn-cog { padding: 6px 8px; min-width: 36px; }

    /* Show the notes toggle button */
    .notes-toggle { display: block; }

    /* Notes list collapsible: hidden by default on mobile */
    .note-list {
        width: 100%;
        max-height: 0;
        overflow: hidden;
        transition: max-height 0.25s ease-out;
        border-right: none;
        border-bottom: 1px solid var(--border);
        flex-shrink: 0;
    }
    .note-list.open {
        max-height: 50vh;
        overflow-y: auto;
    }
    .note-list-header { display: none; } /* redundant on mobile, the toggle is the header */

    /* On mobile, always show the edit pencil since there's no hover */
    .note-item-edit { opacity: 1; }

    /* Editor takes remaining space */
    .editor { flex: 1; min-height: 0; }
    .editor-textarea { font-size: 15px; padding: 14px; }

    /* Footer: keep buttons inline, allow them to shrink slightly */
    .editor-footer { padding: 10px 12px; gap: 6px; flex-wrap: wrap; }
    .editor-footer .btn { font-size: 13px; padding: 8px 12px; }

    /* PIN pad on small screens: tighten up */
    .pin-box { padding: 24px; }
    .demo-banner { padding: 6px 10px; font-size: 11px; }

    /* Settings modal: stack rows vertically on narrow screens */
    .settings-row { flex-direction: column; align-items: stretch; }
    .settings-select { width: 100%; }

    /* Theme toggle on mobile: keep it compact */
    .theme-toggle { padding: 2px 6px; font-size: 13px; }
}