/* ===== CSS Variables ===== */
:root {
    --bg:          #f5f3ef;
    --surface:     #ffffff;
    --surface2:    #f0ece6;
    --border:      #ddd9d3;
    --border-lt:   #eae6e0;
    --ink:         #1c1a17;
    --ink2:        #6b6660;
    --ink3:        #a09b95;
    --accent:      #3c3c3c;
    --accent2:     #7a6a5a;
    --active-bg:   #ede9e3;
    --active-bdr:  #8b7a6a;
    --danger:      #a0522d;
    --r-sm:        3px;
    --r:           5px;
    --shadow:      0 1px 4px rgba(0,0,0,.06), 0 0 1px rgba(0,0,0,.04);
    --shadow-md:   0 2px 12px rgba(0,0,0,.09);
    --toolbar-glass: rgba(255, 255, 255, 0.72);
    --toolbar-border: rgba(255, 255, 255, 0.66);
    --toolbar-shadow: 0 16px 34px rgba(30, 24, 16, .16), 0 3px 10px rgba(30, 24, 16, .12);
}

/* ===== Dark Mode ===== */
[data-theme="dark"] {
    --bg:          #1a1a1e;
    --surface:     #242428;
    --surface2:    #2a2a2e;
    --border:      #3a3a3e;
    --border-lt:   #333338;
    --ink:         #e0ddd8;
    --ink2:        #9a9590;
    --ink3:        #6a6560;
    --accent:      #d0ccc6;
    --accent2:     #a08a7a;
    --active-bg:   #2e2e32;
    --active-bdr:  #8b7a6a;
    --danger:      #c06030;
    --shadow:      0 1px 4px rgba(0,0,0,.2), 0 0 1px rgba(0,0,0,.1);
    --shadow-md:   0 2px 12px rgba(0,0,0,.25);
    --toolbar-glass: rgba(36, 36, 40, 0.76);
    --toolbar-border: rgba(255, 255, 255, 0.12);
    --toolbar-shadow: 0 16px 30px rgba(0, 0, 0, .32), 0 2px 10px rgba(0, 0, 0, .24);
}
[data-theme="dark"] #template-canvas,
[data-theme="dark"] #template-canvas-right { background: #2a2a2e !important; }
[data-theme="dark"] .text-input-overlay { background: rgba(36,36,40,.92); }
[data-theme="dark"] .text-format-toolbar { background: rgba(36,36,40,.95); border-color: var(--border-lt); box-shadow: 0 2px 8px rgba(0,0,0,.3); }
[data-theme="dark"] .note-thumb { background: #2a2a2e; }
[data-theme="dark"] .editor-header { background: linear-gradient(180deg, rgba(36,36,40,.92), rgba(30,30,34,.78)); }

/* ===== Reset & Base ===== */
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
    font-family: 'Noto Sans JP', -apple-system, 'Hiragino Sans', 'Yu Gothic UI', sans-serif;
    font-weight: 300;
    background: var(--bg);
    color: var(--ink);
    height: 100vh;
    overflow: hidden;
    letter-spacing: 0.01em;
    -webkit-font-smoothing: antialiased;
}
.screen { display: none; height: 100vh; }
.screen.active { display: flex; flex-direction: column; }

/* ===== Login ===== */
#login-screen { align-items: center; justify-content: center; }
.login-container {
    text-align: center;
    padding: 52px 48px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--r);
    box-shadow: var(--shadow-md);
    max-width: 380px;
    width: 90%;
    animation: fadeInUp .4s ease-out;
}
.login-container h1 { font-size: 28px; font-weight: 300; letter-spacing: 0.18em; margin-bottom: 6px; }
.login-container p { color: var(--ink2); margin-bottom: 36px; font-size: 13px; letter-spacing: 0.06em; }
.btn-google {
    display: inline-flex; align-items: center; gap: 12px;
    padding: 11px 28px; border: 1px solid var(--border);
    background: var(--surface); font-size: 13px; font-family: inherit;
    color: var(--ink); cursor: pointer; letter-spacing: 0.04em;
    border-radius: var(--r); transition: background .15s, box-shadow .15s, transform .1s;
}
.btn-google:hover { background: var(--surface2); box-shadow: var(--shadow); }
.btn-google:active { transform: scale(0.98); }
.btn-install {
    display: inline-flex; align-items: center; gap: 8px;
    padding: 9px 24px; border: 1px solid var(--border);
    background: transparent; font-size: 12px; font-family: inherit;
    color: var(--ink2); cursor: pointer; letter-spacing: 0.04em;
    margin-top: 10px; transition: background .15s, color .15s;
    width: 100%; justify-content: center; border-radius: var(--r);
}
.btn-install:hover { background: var(--surface2); color: var(--ink); }

/* ===== Animations ===== */
@keyframes fadeInUp {
    from { opacity: 0; transform: translateY(12px); }
    to   { opacity: 1; transform: translateY(0); }
}
@keyframes fadeIn {
    from { opacity: 0; }
    to   { opacity: 1; }
}
@keyframes modalIn {
    from { opacity: 0; transform: scale(0.96) translateY(8px); }
    to   { opacity: 1; transform: scale(1) translateY(0); }
}

/* ===== Buttons ===== */
.btn {
    padding: 7px 16px; border: 1px solid var(--border);
    background: var(--surface); cursor: pointer; font-size: 12px;
    font-family: inherit; font-weight: 400; color: var(--ink);
    letter-spacing: 0.04em; transition: background .15s, border-color .15s, box-shadow .15s, transform .1s;
    border-radius: var(--r);
}
.btn:hover { background: var(--surface2); }
.btn:active { transform: scale(0.97); }
.btn-sm { padding: 4px 12px; font-size: 11px; }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary:hover { background: #555; border-color: #555; }
.btn-secondary { background: var(--surface2); border-color: var(--border); }
.btn-secondary:hover { background: var(--active-bg); }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }

/* ===== Dashboard ===== */
.dashboard-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 0 24px; height: 48px;
    background: var(--surface); border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.dashboard-header h2 { font-size: 13px; font-weight: 400; letter-spacing: 0.2em; text-transform: uppercase; color: var(--ink2); }
.header-actions { display: flex; align-items: center; gap: 10px; }
#user-name { font-size: 11px; color: var(--ink3); letter-spacing: 0.04em; }
.dashboard-body { display: flex; flex: 1; overflow: hidden; }

/* ===== Sidebar ===== */
.sidebar { width: 220px; background: var(--surface2); border-right: 1px solid var(--border); display: flex; flex-direction: column; flex-shrink: 0; transition: width .2s, min-width .2s; overflow: hidden; }
.sidebar.collapsed { width: 0; min-width: 0; border-right-width: 0; }
.sidebar-header { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px 12px; border-bottom: 1px solid var(--border-lt); }
.sidebar-header h3 { font-size: 10px; font-weight: 500; letter-spacing: 0.14em; text-transform: uppercase; color: var(--ink3); }
.folder-tree { padding: 6px 8px; flex: 1; overflow-y: auto; }
.folder-item { display: flex; align-items: center; padding: 7px 8px; border-radius: var(--r-sm); cursor: pointer; font-size: 12px; gap: 6px; user-select: none; color: var(--ink2); transition: background .12s, color .12s, border-color .12s; border-left: 3px solid transparent; }
.folder-item:hover { background: var(--active-bg); color: var(--ink); }
.folder-item.active { background: var(--active-bg); color: var(--ink); font-weight: 500; border-left-color: var(--active-bdr); }
.folder-item .folder-icon { font-size: 14px; opacity: 0.7; }
.folder-item .folder-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder-icon-apple {
    width: 20px;
    height: 15px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    filter: drop-shadow(0 1px 1px rgba(23, 34, 52, 0.24));
}
.folder-icon-apple svg { width: 100%; height: 100%; display: block; }
.folder-item .folder-icon-apple { opacity: 1; }
.folder-icon-apple-lg {
    width: 96px;
    height: 70px;
    filter: drop-shadow(0 6px 10px rgba(23, 34, 52, 0.2));
}
.folder-note-thumb {
    background: linear-gradient(180deg, rgba(255,255,255,0.7), rgba(244,240,233,0.4));
}
.folder-children { padding-left: 14px; }
.folder-actions { display: flex; gap: 2px; opacity: 0; pointer-events: none; transition: opacity .12s; }
.folder-item:hover .folder-actions,
.folder-item:focus-within .folder-actions { opacity: 1; pointer-events: auto; }
.folder-actions button { background: none; border: none; cursor: pointer; font-size: 11px; padding: 2px 5px; border-radius: var(--r-sm); color: var(--ink3); }
.folder-actions button { touch-action: manipulation; }
.folder-actions button:hover { background: var(--border); color: var(--ink); }
.folder-actions button[data-action="folder-menu"] {
    font-size: 16px;
    line-height: 1;
    padding: 1px 4px;
}

/* ===== Main Content ===== */
.main-content { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
.content-header { display: flex; justify-content: space-between; align-items: center; padding: 14px 24px; flex-wrap: wrap; gap: 8px; border-bottom: 1px solid var(--border-lt); background: var(--surface); flex-shrink: 0; }
.breadcrumb { font-size: 11px; color: var(--ink3); letter-spacing: 0.04em; }
.breadcrumb span { cursor: pointer; }
.breadcrumb span:hover { color: var(--ink); }
.content-actions { display: flex; gap: 8px; }

/* ===== Notes Grid ===== */
.notes-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 14px; padding: 20px 24px; overflow-y: auto; flex: 1; align-content: start; }
.note-card { background: var(--surface); padding: 14px; border: 1px solid var(--border-lt); cursor: pointer; transition: border-color .15s, box-shadow .2s, transform .2s; position: relative; border-radius: var(--r); }
.note-card:hover { border-color: var(--border); box-shadow: var(--shadow-md); transform: translateY(-2px); }
.note-card .note-thumb { width: 100%; aspect-ratio: 0.707; background: var(--bg); margin-bottom: 10px; overflow: hidden; display: flex; align-items: center; justify-content: center; }
.note-card .note-thumb img, .note-card .note-thumb canvas { max-width: 100%; max-height: 100%; object-fit: contain; }
.note-card .note-title { font-size: 12px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.note-card .note-date { font-size: 10px; color: var(--ink3); margin-top: 3px; }
.note-card .note-badge { position: absolute; top: 8px; right: 8px; font-size: 9px; background: var(--accent2); color: #fff; padding: 1px 6px; letter-spacing: 0.06em; text-transform: uppercase; border-radius: var(--r-sm); transition: right .2s; }
.note-card .note-delete { position: absolute; top: 8px; left: 8px; opacity: 0; font-size: 11px; background: rgba(160,82,45,.85); color: #fff; border: none; width: 22px; height: 22px; cursor: pointer; display: flex; align-items: center; justify-content: center; border-radius: var(--r-sm); transition: opacity .15s, background .12s; }
.note-card .note-delete:hover { background: var(--danger); }
.note-card:hover .note-delete { opacity: 1; }

.note-card .note-cover-btn { position: absolute; top: 8px; right: 8px; opacity: 0; font-size: 11px; background: rgba(60,60,60,.85); color: #fff; border: none; width: 22px; height: 22px; cursor: pointer; display: flex; align-items: center; justify-content: center; border-radius: var(--r-sm); transition: opacity .15s, background .12s; }
.note-card .note-cover-btn:hover { background: var(--accent); }
.note-card:hover .note-cover-btn { opacity: 1; }
.note-card:hover .note-badge { right: 34px; }
.folder-menu-card-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    border: none;
    background: rgba(255,255,255,0.86);
    color: var(--active-bdr);
    border-radius: 999px;
    width: 24px;
    height: 24px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    line-height: 1;
    box-shadow: 0 1px 4px rgba(0,0,0,.12);
}
.folder-menu-card-btn:hover { background: #fff; transform: translateY(-1px); }

/* Drag & Drop */
.note-card.dragging { opacity: 0.4; }
.note-card.drop-over, .folder-item.drop-over { outline: 2px dashed var(--active-bdr); outline-offset: -2px; background: var(--active-bg); }

/* ===== Modal ===== */
.modal { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(20,18,15,.4); display: flex; align-items: center; justify-content: center; z-index: 1000; backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); animation: fadeIn .15s ease-out; }
.modal-content { background: var(--surface); border: 1px solid var(--border); padding: 32px 36px; max-width: 480px; width: 90%; max-height: 85vh; overflow-y: auto; border-radius: var(--r); box-shadow: var(--shadow-md); animation: modalIn .2s ease-out; }
.modal-content h3 { font-size: 12px; font-weight: 500; letter-spacing: 0.14em; text-transform: uppercase; color: var(--ink2); margin-bottom: 24px; }
.form-group { margin-bottom: 18px; }
.form-group label { display: block; font-size: 10px; font-weight: 500; letter-spacing: 0.12em; text-transform: uppercase; color: var(--ink3); margin-bottom: 8px; }
.input { width: 100%; padding: 9px 12px; border: 1px solid var(--border); background: var(--surface); font-size: 13px; font-family: inherit; color: var(--ink); outline: none; transition: border-color .15s, box-shadow .15s; border-radius: var(--r-sm); }
.input:focus { border-color: var(--active-bdr); box-shadow: 0 0 0 2px rgba(139,122,106,.15); }
.size-options, .template-options { display: flex; gap: 8px; flex-wrap: wrap; }
.size-btn { padding: 6px 18px; border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-size: 12px; font-family: inherit; color: var(--ink2); letter-spacing: 0.06em; transition: all .12s; border-radius: var(--r-sm); }
.size-btn.active { border-color: var(--active-bdr); background: var(--active-bg); color: var(--ink); }
.template-btn { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 10px 12px; border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-size: 11px; font-family: inherit; color: var(--ink2); letter-spacing: 0.04em; transition: all .12s; border-radius: var(--r-sm); }
.template-btn.active { border-color: var(--active-bdr); background: var(--active-bg); color: var(--ink); }
.template-preview { width: 52px; height: 70px; border: 1px solid var(--border-lt); background: #fff; border-radius: var(--r-sm); }
.template-preview.lined { background: repeating-linear-gradient(transparent, transparent 11px, #d8d4cf 12px); }
.template-preview.dot { background-color: #fff; background-image: radial-gradient(circle, #c0bbb5 1px, transparent 1px); background-size: 10px 10px; }
.template-preview.grid { background: repeating-linear-gradient(0deg, transparent, transparent 9px, #d8d4cf 10px), repeating-linear-gradient(90deg, transparent, transparent 9px, #d8d4cf 10px); }
.template-preview.cornell {
    border-color: #cbc6c0;
    background-color: #fff;
    background-image:
        radial-gradient(circle, #c7c2bc 0.7px, transparent 0.8px),
        linear-gradient(#d0cbc4, #d0cbc4),
        linear-gradient(#d0cbc4, #d0cbc4),
        linear-gradient(#d0cbc4, #d0cbc4),
        linear-gradient(#fff, #fff),
        linear-gradient(#fff, #fff),
        linear-gradient(#fff, #fff);
    background-size:
        7px 7px,
        calc(100% - 10px) 1px,
        calc(100% - 10px) 1px,
        1px calc(100% - 24px),
        26% 56%,
        70% 16%,
        70% 24%;
    background-position:
        68% 48%,
        center 16%,
        center 74%,
        31% 45%,
        14% 46%,
        66% 8%,
        66% 86%;
    background-repeat: repeat, no-repeat, no-repeat, no-repeat, no-repeat, no-repeat, no-repeat;
}
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 24px; padding-top: 18px; border-top: 1px solid var(--border-lt); }

/* ===== Editor ===== */
.editor-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 16px;
    height: 44px;
    background: linear-gradient(180deg, rgba(255,255,255,.94), rgba(250,247,242,.82));
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}
.editor-title { flex: 1; font-size: 13px; font-weight: 400; letter-spacing: 0.06em; color: var(--ink2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.editor-actions { display: flex; gap: 6px; }
.readonly-badge { background: var(--accent2); color: #fff; padding: 2px 10px; font-size: 10px; font-weight: 500; letter-spacing: 0.08em; text-transform: uppercase; display: inline-flex; align-items: center; gap: 4px; border-radius: var(--r-sm); }

/* ===== Toolbar ===== */
.toolbar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    min-height: 56px;
    margin: 10px 14px 12px;
    width: calc(100% - 28px);
    align-self: center;
    background: linear-gradient(125deg, var(--toolbar-glass), rgba(255,255,255,.56));
    border: 1px solid var(--toolbar-border);
    border-radius: 999px;
    box-shadow: var(--toolbar-shadow);
    backdrop-filter: blur(18px);
    -webkit-backdrop-filter: blur(18px);
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    flex-shrink: 0;
    position: relative;
    transition: transform .25s ease, opacity .2s ease;
}
.toolbar::-webkit-scrollbar { display: none; }
.toolbar.collapsed { display: none !important; margin: 0 !important; }
.tool-group {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
    padding: 4px;
    border-radius: 999px;
    background: rgba(255,255,255,.28);
    border: 1px solid rgba(255,255,255,.48);
}
.tool-group label {
    font-size: 10px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: .08em;
    color: var(--ink3);
    white-space: nowrap;
    margin-right: 4px;
    margin-left: 2px;
}
.tool-divider {
    width: 1px;
    height: 30px;
    background: linear-gradient(180deg, rgba(0,0,0,0), rgba(0,0,0,.16), rgba(0,0,0,0));
    margin: 0 2px;
    flex-shrink: 0;
}
.tool-btn {
    width: 38px;
    height: 38px;
    border: 1px solid transparent;
    background: rgba(255,255,255,.52);
    cursor: pointer;
    font-size: 15px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background .14s, border-color .14s, color .14s, transform .14s, box-shadow .14s;
    flex-shrink: 0;
    color: var(--ink2);
    border-radius: 50%;
}
.tool-btn svg { width: 18px; height: 18px; flex-shrink: 0; }
.tool-btn:hover {
    background: rgba(255,255,255,.9);
    color: var(--ink);
    transform: translateY(-1px);
    box-shadow: 0 5px 10px rgba(0,0,0,.12);
}
.tool-btn:active { transform: scale(0.95); }
.tool-btn.active {
    border-color: rgba(28, 26, 23, .22);
    background: var(--accent);
    color: #fff;
    box-shadow: 0 8px 14px rgba(28, 26, 23, .28);
}
.mode-btn { font-size: 13px; }
.mode-btn.mode-active { border-color: var(--accent2); background: #f5ede6; color: var(--accent2); }
[data-theme="dark"] .mode-btn.mode-active { background: rgba(160,138,122,.2); }
.size-slider { width: 96px; accent-color: var(--accent2); }
#brush-size-label {
    font-size: 10px;
    color: var(--ink);
    min-width: 28px;
    text-align: center;
    background: rgba(255,255,255,.66);
    border: 1px solid rgba(0,0,0,.08);
    border-radius: 999px;
    padding: 4px 6px;
    font-weight: 500;
}
.size-label { font-size: 10px; color: var(--ink3); min-width: 14px; text-align: right; }
.palette-container { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.palette-header { display: flex; align-items: center; justify-content: center; }
.color-palette { display: flex; gap: 4px; flex-wrap: nowrap; align-items: center; }
.color-btn { width: 20px; height: 20px; border-radius: 50%; border: 1px solid rgba(0,0,0,.14); cursor: pointer; transition: transform .1s; flex-shrink: 0; }
.color-btn:hover { transform: scale(1.15); }
.color-btn.active { border-color: var(--active-bdr); box-shadow: 0 0 0 2px #fff, 0 0 0 4px var(--active-bdr); }
.custom-color-input { width: 22px; height: 22px; border: 1px solid var(--border); border-radius: 50%; cursor: pointer; padding: 0; flex-shrink: 0; overflow: hidden; }
.recent-palette-btn { opacity: 0.9; }

/* ===== Editor Body ===== */
.editor-body { flex: 1; display: flex; overflow: hidden; }
.editor-panel { flex: 1; display: flex; overflow: hidden; min-width: 0; position: relative; }
#panel-right { flex-direction: column; }
.editor-panel .panel-right-header { display: none; }
#panel-right .panel-right-header { display: flex; align-items: center; justify-content: space-between; padding: 4px 10px; background: var(--surface2); border-bottom: 1px solid var(--border); height: 32px; flex-shrink: 0; }
.panel-right-title { font-size: 11px; font-weight: 500; color: var(--ink2); letter-spacing: 0.04em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#panel-right .panel-right-body { flex: 1; display: flex; overflow: hidden; }
.editor-panel.active { box-shadow: inset 0 2px 0 0 var(--active-bdr); }
.panel-divider { width: 4px; background: var(--border); flex-shrink: 0; cursor: col-resize; transition: background .12s; }
.panel-divider:hover { background: var(--active-bdr); }
/* Split note picker */
.split-note-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; max-height: 340px; overflow-y: auto; padding: 8px 0; }
.split-note-item { background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 8px; cursor: pointer; text-align: center; transition: border-color .12s, background .12s; }
.split-note-item:hover { border-color: var(--active-bdr); background: var(--active-bg); }
.split-note-item img { width: 100%; aspect-ratio: 0.707; object-fit: cover; border-radius: 3px; margin-bottom: 6px; background: #fff; }
.split-note-item .split-note-name { font-size: 11px; color: var(--ink2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.page-sidebar { width: 100px; background: var(--surface2); border-right: 1px solid var(--border); overflow-y: auto; padding: 8px 6px; flex-shrink: 0; }
.page-thumbnails { display: flex; flex-direction: column; gap: 8px; }
.page-thumb { position: relative; background: var(--surface); border: 1px solid var(--border); cursor: pointer; overflow: hidden; aspect-ratio: 0.707; transition: border-color .12s, box-shadow .12s; border-radius: var(--r-sm); border-left: 3px solid transparent; }
.page-thumb:hover { border-color: var(--active-bdr); }
.page-thumb.active { border-color: var(--active-bdr); border-left-color: var(--active-bdr); box-shadow: var(--shadow); }
.page-thumb canvas { width: 100%; height: 100%; display: block; }
.page-thumb .page-num { position: absolute; bottom: 2px; right: 4px; font-size: 9px; color: var(--ink3); background: rgba(255,255,255,.85); padding: 1px 4px; border-radius: 2px; }
[data-theme="dark"] .page-thumb .page-num { background: rgba(36,36,40,.85); }
.page-thumb .bookmark-indicator { position: absolute; top: 2px; right: 3px; font-size: 12px; }
.canvas-container { flex: 1; overflow: hidden; position: relative; background: var(--bg); }
#canvas-wrapper { position: absolute; top: 0; left: 0; transform-origin: 0 0; will-change: transform; box-shadow: 0 2px 16px rgba(0,0,0,.12), 0 0 1px rgba(0,0,0,.06); }
#canvas-wrapper canvas, #canvas-wrapper svg { position: absolute; top: 0; left: 0; display: block; }
#canvas-wrapper-right { position: absolute; top: 0; left: 0; transform-origin: 0 0; will-change: transform; box-shadow: 0 2px 16px rgba(0,0,0,.12), 0 0 1px rgba(0,0,0,.06); }
#canvas-wrapper-right canvas, #canvas-wrapper-right svg { position: absolute; top: 0; left: 0; display: block; }
#template-canvas { z-index: 1; background: #fff; pointer-events: none; }
#drawing-svg { z-index: 2; cursor: crosshair; touch-action: none; overflow: hidden; }
#template-canvas-right { z-index: 1; background: #fff; pointer-events: none; }
#drawing-svg-right { z-index: 2; cursor: crosshair; touch-action: none; overflow: hidden; }

/* ===== Image Resize Overlay ===== */
#img-resize-overlay { position: absolute; z-index: 20; border: 1.5px dashed var(--active-bdr); cursor: move; user-select: none; }
#img-resize-overlay img { display: block; width: 100%; height: 100%; pointer-events: none; }
.img-resize-handle { position: absolute; width: 10px; height: 10px; background: var(--surface); border: 1.5px solid var(--active-bdr); border-radius: 50%; }
.img-resize-handle.se { right: -5px; bottom: -5px; cursor: se-resize; }
.img-resize-handle.sw { left: -5px; bottom: -5px; cursor: sw-resize; }
.img-resize-handle.ne { right: -5px; top: -5px; cursor: ne-resize; }
.img-resize-handle.nw { left: -5px; top: -5px; cursor: nw-resize; }
#img-resize-confirm { position: absolute; bottom: -30px; right: 0; display: flex; gap: 4px; }
#img-resize-confirm button { font-size: 10px; padding: 3px 10px; letter-spacing: 0.06em; }

/* ===== Bookmark Panel ===== */
.bookmark-panel { position: fixed; right: 0; top: 88px; width: 220px; background: var(--surface); border-left: 1px solid var(--border); box-shadow: -2px 0 12px rgba(0,0,0,.07); padding: 16px; z-index: 100; height: calc(100vh - 88px); overflow-y: auto; animation: fadeIn .15s ease-out; }
.bookmark-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.bookmark-header h3 { font-size: 10px; font-weight: 500; letter-spacing: 0.14em; text-transform: uppercase; color: var(--ink3); }
.bookmark-list { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.bookmark-item { display: flex; align-items: center; gap: 8px; padding: 7px 8px; cursor: pointer; font-size: 12px; color: var(--ink2); transition: background .12s; border-radius: var(--r-sm); }
.bookmark-item:hover { background: var(--active-bg); }
.bookmark-item .bm-delete { margin-left: auto; opacity: 0; cursor: pointer; background: none; border: none; font-size: 12px; color: var(--ink3); }
.bookmark-item:hover .bm-delete { opacity: 1; }

/* ===== Text input overlay ===== */
.text-input-overlay { position: absolute; z-index: 10; border: 1px solid var(--active-bdr); padding: 4px; font-size: 16px; font-family: inherit; background: rgba(255,255,255,.92); min-width: 80px; outline: none; resize: both; color: var(--ink); }

/* ===== Text format toolbar ===== */
.text-format-toolbar { position: absolute; z-index: 11; display: flex; align-items: center; gap: 6px; padding: 4px 8px; background: rgba(255,255,255,.95); border: 1px solid var(--border-lt); border-radius: 6px; box-shadow: 0 2px 8px rgba(0,0,0,.12); font-size: 12px; white-space: nowrap; }
.text-format-toolbar .fmt-btn { width: 28px; height: 28px; border: 1px solid var(--border-lt); border-radius: 4px; background: var(--surface); cursor: pointer; font-size: 13px; font-weight: 700; display: flex; align-items: center; justify-content: center; color: var(--ink); }
.text-format-toolbar .fmt-btn.active { background: var(--active-bdr); color: #fff; }
.text-format-toolbar .fmt-size { width: 48px; height: 28px; border: 1px solid var(--border-lt); border-radius: 4px; text-align: center; font-size: 12px; background: var(--surface); color: var(--ink); }
.text-format-toolbar .fmt-color { width: 28px; height: 28px; border: 1px solid var(--border-lt); border-radius: 4px; padding: 0; cursor: pointer; background: none; }

/* ===== Lasso selection ===== */
.lasso-selection { position: absolute; z-index: 5; border: 1.5px dashed var(--active-bdr); background: rgba(139,122,106,.05); pointer-events: none; }

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 4px; height: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
::-webkit-scrollbar-thumb:hover { background: var(--ink3); }

/* ===== Responsive Tablet (≤ 1024px) ===== */
@media (max-width: 1024px) {
    .sidebar { position: fixed; left: -230px; top: 0; bottom: 0; z-index: 200; width: 220px; transition: left .2s; box-shadow: 2px 0 16px rgba(0,0,0,.1); overflow-y: auto; }
    .sidebar.open { left: 0; }
    .notes-grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; padding: 16px; }
    .content-header { padding: 12px 16px; }
    .toolbar {
        margin: 8px 10px 10px;
        width: calc(100% - 20px);
        min-height: 52px;
        padding: 6px 10px;
        gap: 6px;
    }
    .tool-group { gap: 3px; padding: 3px; }
    .tool-btn { width: 34px; height: 34px; font-size: 13px; }
    .tool-btn svg { width: 16px; height: 16px; }
    .size-slider { width: 72px; }
    .color-btn { width: 18px; height: 18px; }
    .custom-color-input { width: 20px; height: 20px; }
    .page-sidebar { width: 80px; }
}

/* ===== Responsive Portrait Tablet (≤ 1024px & portrait) ===== */
@media (max-width: 1024px) and (orientation: portrait) {
    .dashboard-header { padding: 0 14px; height: 44px; }
    .dashboard-header h2 { font-size: 12px; }
    .header-actions { gap: 6px; }
    .header-actions .btn-sm { padding: 4px 8px; font-size: 10px; }
    #user-name { font-size: 10px; max-width: 100px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .notes-grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; padding: 14px; }
    .note-card { padding: 10px; }
    .note-card .note-title { font-size: 11px; }
    .content-header { padding: 10px 14px; flex-direction: column; align-items: flex-start; gap: 6px; }
    .content-actions { width: 100%; justify-content: flex-end; }
    .content-actions .btn { font-size: 11px; padding: 5px 10px; }
    .editor-header { height: 40px; padding: 0 10px; gap: 6px; }
    .editor-title { font-size: 12px; }
    .editor-actions .btn-sm { padding: 3px 8px; font-size: 10px; }
    .toolbar {
        min-height: 50px;
        margin: 8px 10px;
        width: calc(100% - 20px);
        padding: 5px 8px;
        gap: 4px;
    }
    .tool-group { gap: 2px; padding: 2px; }
    .tool-btn { width: 32px; height: 32px; font-size: 12px; }
    .tool-btn svg { width: 15px; height: 15px; }
    .size-slider { width: 62px; }
    .palette-container { flex-direction: row; align-items: center; gap: 1px; }
    .color-palette { display: flex; flex-wrap: nowrap; gap: 1px; }
    .color-btn { width: 16px; height: 16px; }
    .color-btn.active { box-shadow: 0 0 0 1.5px #fff, 0 0 0 3px var(--active-bdr); }
    .custom-color-input { width: 18px; height: 18px; }
    .page-sidebar { width: 70px; padding: 6px 4px; }
    .page-thumb .page-num { font-size: 8px; }
    .modal-content { padding: 24px 22px; max-width: 420px; }
    .bookmark-panel { width: 200px; top: 84px; height: calc(100vh - 84px); padding: 12px; }
}

/* ===== Responsive Mobile/Small Tablet (≤ 768px) ===== */
@media (max-width: 768px) {
    #user-name { display: none; }
    .notes-grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; padding: 12px; }
    .editor-header { height: 40px; padding: 0 10px; gap: 8px; }
    .editor-title { font-size: 12px; }
    .editor-actions .btn-sm { padding: 3px 8px; font-size: 10px; }
    .toolbar {
        min-height: 46px;
        margin: 6px 8px 8px;
        width: calc(100% - 16px);
        padding: 4px 6px;
        gap: 4px;
    }
    .tool-group { gap: 2px; padding: 2px; }
    .tool-btn { width: 28px; height: 28px; font-size: 10px; }
    .tool-btn svg { width: 14px; height: 14px; }
    .size-slider { width: 46px; }
    .palette-container { flex-direction: row; align-items: center; gap: 1px; }
    .color-palette { display: flex; flex-wrap: nowrap; gap: 1px; }
    .color-btn { width: 14px; height: 14px; }
    .color-btn.active { box-shadow: 0 0 0 1.5px #fff, 0 0 0 3px var(--active-bdr); }
    .custom-color-input { width: 16px; height: 16px; }
    .tool-group label { display: none; }
    .page-sidebar { width: 68px; }
    .canvas-container { padding: 12px; }
    #split-view-btn { display: none; }
    .modal-content { padding: 22px 20px; }
    .template-options { gap: 6px; }
    .template-btn { padding: 8px; }
    .template-preview { width: 48px; height: 64px; }
}

/* ===== Settings Modal ===== */
.settings-modal-content { max-width: 520px; }
.settings-section { margin-bottom: 24px; }
.settings-section-title { font-size: 10px; font-weight: 500; letter-spacing: 0.14em; text-transform: uppercase; color: var(--ink3); margin-bottom: 14px; padding-bottom: 8px; border-bottom: 1px solid var(--border-lt); }
.settings-desc { font-size: 12px; color: var(--ink2); margin-bottom: 16px; line-height: 1.6; }
.settings-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 12px; }
.settings-label { font-size: 12px; color: var(--ink); flex: 1; }
.settings-sub { font-size: 10px; color: var(--ink3); font-weight: 300; }
.settings-select { padding: 7px 10px; border: 1px solid var(--border); background: var(--surface); font-size: 12px; font-family: inherit; color: var(--ink); outline: none; cursor: pointer; min-width: 200px; border-radius: var(--r-sm); transition: border-color .15s; }
.settings-select:focus { border-color: var(--active-bdr); }
.settings-hint { font-size: 11px; color: var(--ink3); background: var(--surface2); border: 1px solid var(--border-lt); padding: 10px 12px; line-height: 1.6; margin-top: 8px; border-radius: var(--r-sm); }
.settings-hint strong { color: var(--ink2); }

/* Toggle switch */
.toggle-switch { position: relative; display: inline-block; width: 40px; height: 22px; flex-shrink: 0; }
.toggle-switch input { opacity: 0; width: 0; height: 0; }
.toggle-slider { position: absolute; cursor: pointer; inset: 0; background: var(--border); border-radius: 22px; transition: background .2s; }
.toggle-slider::before { content: ''; position: absolute; width: 16px; height: 16px; left: 3px; bottom: 3px; background: var(--surface); border-radius: 50%; transition: transform .2s; }
.toggle-switch input:checked + .toggle-slider { background: var(--active-bdr); }
.toggle-switch input:checked + .toggle-slider::before { transform: translateX(18px); }

/* ===== Focus Indicators (Accessibility) ===== */
.btn:focus-visible,
.tool-btn:focus-visible,
.color-btn:focus-visible,
.size-btn:focus-visible,
.template-btn:focus-visible,
.folder-item:focus-visible,
.note-card:focus-visible,
.page-thumb:focus-visible,
.settings-select:focus-visible,
.toggle-switch input:focus-visible + .toggle-slider {
    outline: 2px solid var(--active-bdr);
    outline-offset: 2px;
}
.input:focus-visible {
    outline: none;
    border-color: var(--active-bdr);
    box-shadow: 0 0 0 2px rgba(139,122,106,.2);
}

/* ===== Dark Mode extras ===== */
[data-theme="dark"] .modal { background: rgba(10,10,12,.55); }
[data-theme="dark"] .template-preview { background: #3a3a3e; }
[data-theme="dark"] .template-preview.lined { background: repeating-linear-gradient(transparent, transparent 11px, #4a4a4e 12px); background-color: #3a3a3e; }
[data-theme="dark"] .template-preview.dot { background-color: #3a3a3e; background-image: radial-gradient(circle, #5a5a5e 1px, transparent 1px); background-size: 10px 10px; }
[data-theme="dark"] .template-preview.grid { background-color: #3a3a3e; background-image: repeating-linear-gradient(0deg, transparent, transparent 9px, #4a4a4e 10px), repeating-linear-gradient(90deg, transparent, transparent 9px, #4a4a4e 10px); }
[data-theme="dark"] .template-preview.cornell {
    border-color: #56565a;
    background-color: #3a3a3e;
    background-image:
        radial-gradient(circle, #66666b 0.7px, transparent 0.8px),
        linear-gradient(#545459, #545459),
        linear-gradient(#545459, #545459),
        linear-gradient(#545459, #545459),
        linear-gradient(#3a3a3e, #3a3a3e),
        linear-gradient(#3a3a3e, #3a3a3e),
        linear-gradient(#3a3a3e, #3a3a3e);
    background-size:
        7px 7px,
        calc(100% - 10px) 1px,
        calc(100% - 10px) 1px,
        1px calc(100% - 24px),
        26% 56%,
        70% 16%,
        70% 24%;
    background-position:
        68% 48%,
        center 16%,
        center 74%,
        31% 45%,
        14% 46%,
        66% 8%,
        66% 86%;
    background-repeat: repeat, no-repeat, no-repeat, no-repeat, no-repeat, no-repeat, no-repeat;
}
[data-theme="dark"] .split-note-item img { background: #2a2a2e; }
[data-theme="dark"] .color-btn.active { box-shadow: 0 0 0 2px var(--surface), 0 0 0 3.5px var(--active-bdr); }
@media (max-width: 1024px) and (orientation: portrait) {
    [data-theme="dark"] .color-btn.active { box-shadow: 0 0 0 1.5px var(--surface), 0 0 0 3px var(--active-bdr); }
}
@media (max-width: 768px) {
    [data-theme="dark"] .color-btn.active { box-shadow: 0 0 0 1.5px var(--surface), 0 0 0 3px var(--active-bdr); }
}
[data-theme="dark"] ::-webkit-scrollbar-thumb { background: var(--border); }
[data-theme="dark"] ::-webkit-scrollbar-thumb:hover { background: var(--ink3); }

/* ===== Touch Coarse ===== */
@media (pointer: coarse) {
    .toolbar .tool-btn { min-width: auto; min-height: auto; }
    .folder-item { padding: 10px 8px; min-height: 44px; }
    .btn { min-height: 44px; display: inline-flex; align-items: center; justify-content: center; }
    .btn-sm { min-height: 36px; }
    .note-card .note-delete { width: 28px; height: 28px; opacity: 1; }
    .note-card .note-cover-btn { width: 28px; height: 28px; opacity: 1; }
    .note-card .note-badge { right: 40px; }
    .folder-actions { opacity: 1; pointer-events: auto; }
    .folder-actions button[data-action="folder-menu"] { font-size: 18px; min-width: 30px; min-height: 30px; }
    .folder-menu-card-btn { width: 30px; height: 30px; font-size: 20px; }
}

/* ===== Cover Picker Modal ===== */
.cover-grid { 
    display: grid; 
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); 
    gap: 15px; 
    margin: 15px 0; 
    max-height: 300px; 
    overflow-y: auto; 
    padding: 5px; 
}
.cover-option {
    width: 100%;
    aspect-ratio: 3/4;
    border-radius: var(--r-md);
    cursor: pointer;
    border: 2px solid transparent;
    transition: all 0.2s;
    background-size: cover;
    background-position: center;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.cover-option:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.15);
}
.cover-option.active {
    border-color: var(--active-bdr);
    box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--active-bdr);
}

/* ===== Context Menu ===== */
.context-menu {
    position: fixed;
    z-index: 99999;
    min-width: 180px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--r-md);
    box-shadow: 0 8px 24px rgba(0,0,0,0.18), 0 2px 8px rgba(0,0,0,0.08);
    padding: 4px 0;
    font-size: 13px;
    user-select: none;
}
.context-menu-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    cursor: pointer;
    color: var(--ink);
    white-space: nowrap;
    transition: background .1s;
}
.context-menu-item-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    flex: 0 0 16px;
}
.context-menu-item-icon img {
    width: 16px;
    height: 16px;
    display: block;
}
.context-menu-item:hover {
    background: var(--surface2);
}
.context-menu-item.danger {
    color: var(--danger, #e74c3c);
}
.context-menu-item.danger:hover {
    background: rgba(231,76,60,0.1);
}
.context-menu-divider {
    height: 1px;
    background: var(--border);
    margin: 4px 0;
}

/* Keep folder action buttons reachable on touch devices */
@media (pointer: coarse) {
    .folder-actions {
        opacity: 1;
        pointer-events: auto;
    }
    .folder-actions button {
        min-width: 28px;
        min-height: 28px;
    }
}

/* ===== Star / Favorites ===== */
.note-card .note-star {
    position: absolute; top: 32px; left: 8px; opacity: 0;
    font-size: 16px; background: none; color: #bbb;
    border: none; width: 22px; height: 22px; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: opacity .15s, color .12s;
    line-height: 1;
}
.note-card .note-star.starred { color: #f5a623; opacity: 1; }
.note-card:hover .note-star { opacity: 1; }
.note-card .note-star:hover { color: #f5a623; }

.favorites-item {
    display: flex; align-items: center; gap: 6px;
    padding: 7px 12px; cursor: pointer;
    border-radius: var(--r-sm);
    transition: background .12s;
    font-size: 13px; color: var(--ink);
    margin: 4px 6px;
}
.favorites-item:hover { background: var(--surface2); }
.favorites-item.active { background: var(--active-bg); font-weight: 500; border-left: 3px solid var(--active-bdr); }
.favorites-item .folder-icon { font-size: 15px; }
.favorites-item .folder-name { flex: 1; }

/* ===== Custom Palette ===== */
.palette-container { display: flex; flex-direction: row; gap: 8px; align-items: center; }
.palette-header { display: flex; align-items: center; justify-content: center; }
.palette-preset-select {
    font-size: 11px; padding: 7px 28px 7px 11px;
    border: 1px solid rgba(0,0,0,.08); border-radius: 999px;
    background-color: rgba(255,255,255,.62); color: var(--ink);
    cursor: pointer; outline: none;
    -webkit-appearance: none; appearance: none;
    background-image: url('data:image/svg+xml;utf8,<svg fill="none" stroke="%23777" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><polyline points="6 9 12 15 18 9"/></svg>');
    background-repeat: no-repeat;
    background-position: right 10px center;
    background-size: 10px;
    transition: all 0.2s ease;
    font-weight: 500;
}
.palette-preset-select:hover {
    border-color: var(--active-bdr);
    background-color: rgba(255,255,255,.86);
}
.palette-preset-select:focus {
    border-color: var(--active-bdr);
    box-shadow: 0 0 0 2px rgba(139,122,106,.2);
}
.recent-palette-btn {
    border-style: dashed;
}

/* ===== Page Thumb Drag & Drop ===== */
.page-thumb[draggable] { cursor: grab; }
.page-thumb.dragging { opacity: 0.3; outline: 2px dashed var(--active-bdr); }
.page-thumb.drop-target { border-top: 3px solid #4A90D9; }

/* ===== Touch/mobile overrides for new features ===== */
@media (pointer: coarse) {
    .note-card .note-star { opacity: 1; width: 28px; height: 28px; font-size: 18px; }
    .palette-preset-select {
        font-size: 14px;
        padding: 7px 30px 7px 14px;
        background-position: right 10px center;
        background-size: 14px;
        border-radius: 999px;
    }
}
@media (max-width: 768px) {
    .recent-palette-btn { opacity: 0.85; }
}

/* ===== Dark mode additions ===== */
[data-theme="dark"] .note-card .note-star { color: #666; }
[data-theme="dark"] .note-card .note-star.starred { color: #f5a623; }
[data-theme="dark"] .favorites-item:hover { background: var(--surface2); }
[data-theme="dark"] .tool-group {
    background: rgba(255,255,255,.04);
    border-color: rgba(255,255,255,.1);
}
[data-theme="dark"] .tool-btn {
    background: rgba(255,255,255,.08);
    border-color: rgba(255,255,255,.06);
    color: #d9d6d1;
}
[data-theme="dark"] .tool-btn:hover {
    background: rgba(255,255,255,.16);
    color: #fff;
}
[data-theme="dark"] .tool-btn.active {
    background: #d0ccc6;
    color: #262629;
    border-color: rgba(208,204,198,.6);
    box-shadow: 0 8px 14px rgba(0,0,0,.28);
}
[data-theme="dark"] #brush-size-label {
    background: rgba(0,0,0,.34);
    border-color: rgba(255,255,255,.14);
    color: #efebe6;
}
[data-theme="dark"] .palette-preset-select { 
    background-color: rgba(0,0,0,.32); color: var(--ink); border-color: rgba(255,255,255,.12); 
    background-image: url('data:image/svg+xml;utf8,<svg fill="none" stroke="%23aaaaaa" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><polyline points="6 9 12 15 18 9"/></svg>');
}
[data-theme="dark"] .palette-preset-select:hover { background-color: rgba(255,255,255,.08); }
[data-theme="dark"] .recent-color-btn { border-color: rgba(255,255,255,0.2); }
