/* ══════════════════════════════════════════
   TheeWrite — Premium Minimalist Styles
══════════════════════════════════════════ */

/* ── Local fonts (add .ttf files to /fonts/ folder) ── */
@font-face {
    font-family: 'Lato';
    src: local('Lato Regular'), local('Lato-Regular'),
        url('./fonts/Lato-Regular.ttf') format('truetype');
    font-weight: 400;
    font-style: normal;
    font-display: swap;
}

@font-face {
    font-family: 'Lato';
    src: local('Lato Light'), local('Lato-Light'),
        url('./fonts/Lato-Light.ttf') format('truetype');
    font-weight: 300;
    font-style: normal;
    font-display: swap;
}

@font-face {
    font-family: 'Lato';
    src: local('Lato Italic'), local('Lato-Italic'),
        url('./fonts/Lato-Italic.ttf') format('truetype');
    font-weight: 400;
    font-style: italic;
    font-display: swap;
}

/* ── Design tokens ── */
:root {
    /* Palette — deeper warm cream */
    --bg: #ebe6db;
    --bg-editor: #f8f5f0;
    --bg-sidebar: #eee9df;
    --bg-toolbar: #e4ddce;
    --ink: #1a1208;
    --ink-mid: #4a3828;
    --ink-faint: rgba(74, 56, 40, .45);
    --ink-ghost: rgba(74, 56, 40, .22);
    --accent: #8b5e3c;
    --accent-soft: rgba(139, 94, 60, .12);
    --border: rgba(139, 94, 60, .14);
    --border-md: rgba(139, 94, 60, .26);

    /* Spacing */
    --max-w: 1200px;
    /* editor prose width */
    --pad-x: 2rem;
    --tb-h: 50px;

    /* Typography */
    --f-sans: 'Lato', 'Outfit', system-ui, sans-serif;
    --f-serif: 'Playfair Display', Georgia, serif;
    --f-mono: 'Fira Code', 'Courier New', monospace;

    /* Motion */
    --ease: cubic-bezier(.4, 0, .2, 1);
    --dur: .2s;
    --dur-md: .28s;

    /* Elevation */
    --sh-sm: 0 1px 4px rgba(0, 0, 0, .06);
    --sh: 0 4px 20px rgba(0, 0, 0, .08);
    --sh-lg: 0 12px 40px rgba(0, 0, 0, .12);
}

/* ── Reset ── */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html,
body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: var(--f-sans);
    background: var(--bg);
    color: var(--ink);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

button {
    font-family: inherit;
    border: none;
    background: none;
    cursor: pointer;
}

input,
select,
textarea {
    font-family: inherit;
}

/* ══════════════════════════════════════════
   APP SHELL
══════════════════════════════════════════ */
#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
}

/* ══════════════════════════════════════════
   EDITOR AREA
══════════════════════════════════════════ */
#editor-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* Centered column, same as inspo */
    max-width: calc(var(--max-w) + var(--pad-x) * 2);
    width: 100%;
    margin: 0 auto;
    padding: 1.5rem var(--pad-x) 0;
}

/* ── Meta bar (word count + tags) ── */
#meta-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: .65rem;
    flex-shrink: 0;
    min-height: 28px;
}

#meta-left {
    display: flex;
    align-items: center;
    gap: .9rem;
    flex-shrink: 0;
}

#word-count {
    font-size: .78rem;
    color: var(--ink-faint);
    letter-spacing: .3px;
    font-variant-numeric: tabular-nums;
    transition: color var(--dur) var(--ease);
}

#timer {
    font-size: .78rem;
    font-family: var(--f-mono);
    color: var(--ink-mid);
    letter-spacing: .5px;
    font-variant-numeric: tabular-nums;
    transition: color var(--dur);
}

#timer.warn {
    color: #b07c3a;
}

#timer.danger {
    color: #a84832;
    animation: pulse .9s ease infinite;
}

#meta-right {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

/* ── Tag pills ── */
#tag-pills {
    display: flex;
    flex-wrap: wrap;
    gap: 5px;
}

.tag-pill {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    background: var(--accent-soft);
    color: var(--accent);
    border: 1px solid rgba(139, 94, 60, .2);
    font-size: .72rem;
    padding: 2px 9px 2px 10px;
    border-radius: 20px;
    font-weight: 500;
    letter-spacing: .15px;
    transition: background var(--dur);
}

.tag-pill:hover {
    background: rgba(139, 94, 60, .18);
}

.tag-pill button {
    background: none;
    border: none;
    color: inherit;
    opacity: .6;
    cursor: pointer;
    font-size: 13px;
    line-height: 1;
    padding: 0;
    margin-left: 1px;
}

.tag-pill button:hover {
    opacity: 1;
}

#tag-input {
    background: transparent;
    border: 1px solid var(--ink-ghost);
    border-radius: 20px;
    padding: 3px 12px;
    font-size: .75rem;
    color: var(--ink);
    outline: none;
    width: 100px;
    transition: border-color var(--dur), width var(--dur) var(--ease), background var(--dur);
}

#tag-input:hover {
    border-color: var(--border-md);
}

#tag-input:focus {
    border-color: var(--accent);
    background: rgba(255, 255, 255, .7);
    width: 140px;
}

#tag-input::placeholder {
    color: var(--ink-ghost);
    font-style: italic;
}

/* ── Editor wrap + textarea ── */
#editor-wrap {
    flex: 1;
    display: flex;
    overflow: hidden;
}

#editor {
    flex: 1;
    width: 100%;
    resize: none;
    border: none;
    outline: none;
    background: var(--bg-editor);
    border-radius: 10px 10px 0 0;
    padding: 2.5rem 3rem;
    font-size: 18px;
    font-family: 'Lato', Georgia, serif;
    color: var(--ink);
    line-height: 1.85;
    letter-spacing: .08px;
    overflow-y: auto;
    caret-color: var(--accent);
    box-shadow: var(--sh);
    /* No transition on textarea — causes lag */
}

#editor::placeholder {
    color: var(--ink-ghost);
    font-style: italic;
    font-weight: 300;
}

#editor::-webkit-scrollbar {
    width: 5px;
}

#editor::-webkit-scrollbar-track {
    background: transparent;
}

#editor::-webkit-scrollbar-thumb {
    background: var(--border-md);
    border-radius: 3px;
}

/* Font families applied to editor and reread */
#editor.font-Lato,
#reread-content.font-Lato {
    font-family: 'Lato', system-ui, sans-serif;
}

#editor.font-SpecialElite,
#reread-content.font-SpecialElite {
    font-family: 'Special Elite', 'Courier New', monospace;
    letter-spacing: -.3px;
}

#editor.font-CourierPrime,
#reread-content.font-CourierPrime {
    font-family: 'Courier Prime', 'Courier New', monospace;
}

#editor.font-Playfair,
#reread-content.font-Playfair {
    font-family: 'Playfair Display', Georgia, serif;
}

#editor.font-Baskerville,
#reread-content.font-Baskerville {
    font-family: 'Libre Baskerville', Georgia, serif;
}

/* ══════════════════════════════════════════
   BOTTOM TOOLBAR  (inspo-style)
══════════════════════════════════════════ */
#toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 var(--pad-x);
    height: var(--tb-h);
    background: var(--bg-toolbar);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
    user-select: none;
    gap: 1rem;
    /* Match inspo's font size */
    font-size: 13px;
    color: var(--ink-faint);
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none;
    /* Firefox */
}

#toolbar::-webkit-scrollbar {
    display: none;
    /* Chrome/Safari */
}

#tb-left,
#tb-right {
    display: flex;
    align-items: center;
    gap: 0;
    flex-wrap: nowrap;
    flex-shrink: 0;
    /* Prevent squishing */
}

.tb-sep {
    color: var(--ink-ghost);
    padding: 0 5px;
    flex-shrink: 0;
    pointer-events: none;
}

.tb-item {
    background: none;
    border: none;
    color: var(--ink-faint);
    font-size: 13px;
    padding: 4px 3px;
    cursor: pointer;
    transition: color var(--dur);
    white-space: nowrap;
    flex-shrink: 0;
    letter-spacing: .1px;
}

.tb-item:hover {
    color: var(--ink);
}

.tb-item.active {
    color: var(--ink);
    font-weight: 600;
}

.tb-save {
    color: var(--accent);
    font-weight: 600;
}

.tb-save:hover {
    color: var(--ink);
}

/* ── Font size popup ── */
.popup {
    position: fixed;
    background: #fff;
    border: 1px solid var(--border-md);
    border-radius: 8px;
    box-shadow: var(--sh);
    z-index: 500;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    bottom: calc(var(--tb-h) + 8px);
}

.size-opt {
    background: none;
    border: none;
    padding: 9px 20px;
    text-align: left;
    cursor: pointer;
    font-size: 13px;
    color: var(--ink-mid);
    transition: background var(--dur);
    white-space: nowrap;
}

.size-opt:hover {
    background: var(--bg);
    color: var(--ink);
}

/* ══════════════════════════════════════════
   HISTORY SIDEBAR
══════════════════════════════════════════ */
#sidebar {
    position: fixed;
    top: 0;
    right: 0;
    width: 360px;
    height: 100vh;
    background: var(--bg-sidebar);
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    transform: translateX(100%);
    transition: transform var(--dur-md) var(--ease);
    will-change: transform;
    z-index: 200;
    box-shadow: var(--sh-lg);
}

#sidebar.open {
    transform: translateX(0);
}

#sidebar-head {
    display: flex;
    align-items: center;
    gap: .5rem;
    padding: 1rem 1.25rem .75rem;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

#sidebar-head h2 {
    font-size: .9rem;
    font-weight: 600;
    color: var(--ink-mid);
    flex: 1;
    letter-spacing: .2px;
}

#sidebar-tabs {
    display: flex;
    gap: 4px;
    flex-shrink: 0;
}

.stab {
    padding: 3px 10px;
    border-radius: 20px;
    font-size: .72rem;
    color: var(--ink-faint);
    border: 1px solid var(--border);
    transition: all var(--dur);
    letter-spacing: .2px;
    font-weight: 500;
}

.stab.active {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}

#sidebar-search-row {
    display: flex;
    gap: .5rem;
    padding: .65rem 1.25rem;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    flex-wrap: wrap;
}

#search-notes {
    flex: 1;
    padding: .4rem .75rem;
    border: 1px solid var(--border-md);
    border-radius: 6px;
    background: #fff;
    color: var(--ink);
    font-size: .82rem;
    outline: none;
    transition: border-color var(--dur);
}

#search-notes:focus {
    border-color: var(--accent);
}

.custom-select {
    position: relative;
    min-width: 110px;
    max-width: 140px;
}

.select-selected {
    padding: .38rem 1.8rem .38rem .8rem;
    border: 1px solid var(--border-md);
    border-radius: 6px;
    background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%238b5e3c' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E") no-repeat right .5rem center;
    background-size: 12px;
    color: var(--ink-mid);
    font-size: .78rem;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: border-color var(--dur), box-shadow var(--dur);
}

.select-selected:hover {
    border-color: var(--accent);
}

.custom-select.active .select-selected {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-soft);
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
}

.select-options {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--bg-editor);
    border: 1px solid var(--accent);
    border-top: none;
    border-radius: 0 0 6px 6px;
    box-shadow: var(--sh);
    z-index: 100;
    max-height: 200px;
    overflow-y: auto;
}

.select-options div {
    padding: .45rem .8rem;
    font-size: .78rem;
    color: var(--ink-mid);
    cursor: pointer;
    transition: background var(--dur), color var(--dur);
}

.select-options div:hover {
    background: var(--bg-toolbar);
    color: var(--ink);
}

.select-options div.selected {
    background: var(--accent-soft);
    color: var(--accent);
    font-weight: 600;
}

.sidebar-view {
    flex: 1;
    overflow-y: auto;
    padding: .6rem;
}

.sidebar-view::-webkit-scrollbar {
    width: 4px;
}

.sidebar-view::-webkit-scrollbar-thumb {
    background: var(--border-md);
    border-radius: 2px;
}

/* ── Note items ── */
.note-item {
    padding: .85rem 1rem .8rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-bottom: .5rem;
    background: #fff;
    cursor: pointer;
    transition: border-color var(--dur), box-shadow var(--dur);
}

.note-item:hover {
    border-color: var(--border-md);
    box-shadow: var(--sh-sm);
}

.note-item.active-note {
    border-color: var(--accent);
}

.note-meta {
    display: flex;
    justify-content: space-between;
    font-size: .7rem;
    color: var(--ink-ghost);
    margin-bottom: .3rem;
}

.note-title {
    font-size: .85rem;
    color: var(--ink-mid);
    font-weight: 500;
    margin-bottom: .4rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.note-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 3px;
    margin-bottom: .5rem;
}

.tag-chip {
    font-size: .68rem;
    padding: 1px 7px;
    background: var(--accent-soft);
    color: var(--accent);
    border-radius: 20px;
    font-weight: 500;
}

.note-actions {
    display: flex;
    flex-wrap: wrap;
    gap: .35rem;
    opacity: 0;
    transition: opacity var(--dur);
}

.note-item:hover .note-actions {
    opacity: 1;
}

.note-btn {
    padding: .2rem .4rem;
    border: 1px solid var(--border-md);
    border-radius: 5px;
    background: var(--bg);
    color: var(--ink-mid);
    font-size: .65rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .3px;
    cursor: pointer;
    transition: background var(--dur);
}

.note-btn:hover {
    background: var(--bg-toolbar);
}

.note-btn.danger {
    color: #a84832;
    border-color: rgba(168, 72, 50, .2);
}

.note-btn.danger:hover {
    background: rgba(168, 72, 50, .07);
}

.empty-state {
    text-align: center;
    color: var(--ink-ghost);
    font-size: .85rem;
    padding: 3rem 1rem;
    font-style: italic;
    line-height: 1.8;
}

/* ── Tag Cloud ── */
#tag-cloud-view {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-content: flex-start;
    padding: 1.25rem;
}

.cloud-tag {
    cursor: pointer;
    color: var(--accent);
    font-weight: 500;
    transition: color var(--dur), transform .12s;
    line-height: 1.3;
    display: inline-block;
}

.cloud-tag:hover {
    color: var(--ink);
    transform: scale(1.05);
}

.cloud-tag .cloud-count {
    font-size: .65em;
    vertical-align: super;
    opacity: .6;
    margin-left: 2px;
}

/* ══════════════════════════════════════════
   REREAD MODE
══════════════════════════════════════════ */
#reread-panel {
    position: fixed;
    inset: 0;
    background: #faf8f5;
    z-index: 600;
    display: flex;
    flex-direction: column;
    animation: fadeSlideIn .3s var(--ease);
}

#reread-panel.hidden {
    display: none;
}

#reread-head {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 1.2rem 2.5rem;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

#reread-title {
    font-family: var(--f-serif);
    font-size: 1.05rem;
    color: var(--ink-mid);
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

#reread-meta {
    font-size: .75rem;
    color: var(--ink-faint);
    white-space: nowrap;
    flex-shrink: 0;
}

#reread-body {
    flex: 1;
    overflow-y: auto;
    display: flex;
    justify-content: center;
    padding: 4rem 2.5rem;
}

#reread-content {
    max-width: 640px;
    width: 100%;
    font-family: 'Playfair Display', Georgia, serif;
    font-size: 20px;
    line-height: 2;
    color: var(--ink);
    white-space: pre-wrap;
    word-break: break-word;
}

#reread-nav {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 2rem;
    padding: 1.2rem;
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

#reread-nav button {
    font-size: .82rem;
    color: var(--ink-faint);
    padding: .35rem .8rem;
    border-radius: 6px;
    border: 1px solid var(--border);
    transition: all var(--dur);
}

#reread-nav button:hover {
    color: var(--ink);
    border-color: var(--border-md);
    background: #fff;
}

#reread-count {
    font-size: .78rem;
    color: var(--ink-ghost);
    letter-spacing: .3px;
}

/* ══════════════════════════════════════════
   MODAL
══════════════════════════════════════════ */
.modal {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 400;
}

.modal.hidden {
    display: none;
}

.modal-box {
    background: #fff;
    border: 1px solid var(--border-md);
    border-radius: 12px;
    box-shadow: var(--sh-lg);
    width: 400px;
    max-width: 92vw;
}

.modal-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1.1rem 1.5rem .9rem;
    border-bottom: 1px solid var(--border);
}

.modal-head h2 {
    font-size: .95rem;
    font-weight: 600;
    color: var(--ink);
}

.modal-body {
    padding: 1.25rem 1.5rem 1.5rem;
    display: flex;
    flex-direction: column;
    gap: .85rem;
}

.setting-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1.5rem;
}

.setting-row label {
    font-size: .82rem;
    color: var(--ink-mid);
    min-width: 110px;
}

.setting-control {
    display: flex;
    align-items: center;
    gap: .5rem;
}

.setting-control span {
    font-size: .78rem;
    color: var(--ink-faint);
    white-space: nowrap;
}

.setting-control input[type="range"] {
    width: 130px;
    accent-color: var(--accent);
}

.setting-control input[type="number"] {
    width: 60px;
    padding: .3rem .5rem;
    border: 1px solid var(--border-md);
    border-radius: 6px;
    background: var(--bg);
    color: var(--ink);
    font-size: .82rem;
    outline: none;
    text-align: center;
    transition: border-color var(--dur);
}

.setting-control input[type="number"]:focus {
    border-color: var(--accent);
}

.setting-control input[type="checkbox"] {
    accent-color: var(--accent);
    width: 16px;
    height: 16px;
    cursor: pointer;
}

/* ── Overlay ── */
#overlay {
    position: fixed;
    inset: 0;
    background: rgba(10, 6, 3, .35);
    z-index: 190;
    opacity: 0;
    visibility: hidden;
    transition: opacity var(--dur-md), visibility var(--dur-md);
    will-change: opacity;
}

#overlay.active {
    opacity: 1;
    visibility: visible;
}

/* ── Icon button ── */
.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    border: none;
    background: none;
    color: var(--ink-faint);
    cursor: pointer;
    border-radius: 6px;
    transition: color var(--dur), background var(--dur);
    flex-shrink: 0;
}

.icon-btn svg {
    width: 16px;
    height: 16px;
    stroke: currentColor;
    fill: none;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.icon-btn:hover {
    color: var(--ink);
    background: var(--bg-toolbar);
}

/* ── Toast ── */
#toast {
    position: fixed;
    bottom: calc(var(--tb-h) + 1.5rem);
    left: 50%;
    transform: translateX(-50%) translateY(6px);
    background: var(--ink);
    color: var(--bg);
    padding: .5rem 1.2rem;
    border-radius: 20px;
    font-size: .78rem;
    font-weight: 500;
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s, transform .2s var(--ease);
    will-change: opacity, transform;
    white-space: nowrap;
    z-index: 700;
}

#toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

#toast.success {
    background: #437a32;
}

#toast.warn {
    background: #9c6c2a;
}

#toast.danger {
    background: #9c3c28;
}

/* ── Print area (for PDF export) ── */
#print-area {
    display: none;
}

@media print {
    body>* {
        display: none !important;
    }

    #print-area {
        display: block !important;
        padding: 2.5cm 3cm;
        font-family: 'Playfair Display', Georgia, serif;
        font-size: 12pt;
        line-height: 1.9;
        color: #000;
        white-space: pre-wrap;
    }

    #print-area .print-title {
        font-size: 16pt;
        font-weight: 600;
        margin-bottom: 2rem;
        padding-bottom: .75rem;
        border-bottom: 1px solid #ccc;
    }

    #print-area .print-meta {
        font-size: 9pt;
        color: #888;
        margin-bottom: 2rem;
    }
}

/* ── Utility ── */
.hidden {
    display: none !important;
}

/* ── Animations ── */
@keyframes pulse {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: .4;
    }
}

@keyframes fadeSlideIn {
    from {
        opacity: 0;
        transform: translateY(6px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* ══════════════════════════════════════════
   RESPONSIVE
══════════════════════════════════════════ */
@media (max-width: 700px) {
    :root {
        --pad-x: 1rem;
        --max-w: 100%;
    }

    #editor {
        padding: 1.5rem 1.25rem;
    }

    #sidebar {
        width: 100%;
    }

    #tb-left .font-opt {
        display: none;
    }

    #tb-left .font-opt:first-of-type {
        display: block;
    }

    /* show only first */
    #reread-content {
        font-size: 17px;
    }

    #reread-body {
        padding: 2rem 1.5rem;
    }

    .setting-row {
        flex-wrap: wrap;
        gap: .5rem;
        justify-content: flex-start;
    }

    .setting-row label {
        min-width: 100%;
        margin-bottom: -0.2rem;
    }

    .setting-control {
        width: 100%;
        justify-content: space-between;
    }

    .setting-control input[type="range"] {
        flex: 1;
        margin-right: .5rem;
    }

    .custom-select {
        flex: 1;
        max-width: 100%;
    }
}