:root {
    --bg: #ffffff;
    --surface: #ffffff;
    --sidebar: #f7f7f5;
    --panel: #fbfbfa;
    --hover: #efefed;
    --active: #e9e9e7;
    --text: #2f3437;
    --muted: #787774;
    --muted-2: #a3a29e;
    --line: #e9e9e7;
    --accent: #6d5dfc;
    --accent-hover: #5f50e8;
    --accent-soft: #efedff;
    --danger: #d44c47;
    --danger-soft: #fff1f0;
    --shadow: 0 8px 30px rgba(15, 15, 15, .08);
    --font: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

:root[data-theme="dark"] {
    --bg: #191919;
    --surface: #202020;
    --sidebar: #202020;
    --panel: #1d1d1d;
    --hover: #2b2b2b;
    --active: #313131;
    --text: #ebebea;
    --muted: #a4a4a0;
    --muted-2: #777773;
    --line: #303030;
    --accent: #8b7cff;
    --accent-hover: #9a8dff;
    --accent-soft: #2f2a4b;
    --danger: #ff736d;
    --danger-soft: #3a2625;
    --shadow: 0 10px 38px rgba(0, 0, 0, .28);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body { font-family: var(--font); background: var(--bg); color: var(--text); -webkit-font-smoothing: antialiased; }
button, input, textarea, select { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
a { color: inherit; text-decoration: none; }
button { color: inherit; }
[hidden] { display: none !important; }

/* --- App shell, Notion-like --- */
.app-body { min-height: 100vh; overflow: hidden; }
.notion-shell {
    min-height: 100vh;
    display: grid;
    grid-template-columns: 232px 310px minmax(0, 1fr);
    background: var(--bg);
}

.sidebar {
    height: 100vh;
    background: var(--sidebar);
    border-right: 1px solid var(--line);
    padding: 12px 8px 10px;
    display: flex;
    flex-direction: column;
    min-width: 0;
}
.workspace-name { padding: 8px 8px 12px; position: relative; }
.workspace-name strong { display: block; font-size: 14px; line-height: 1.35; font-weight: 700; letter-spacing: -.01em; }
.workspace-name span { display: block; color: var(--muted); font-size: 11px; margin-top: 2px; }
.mobile-close { display: none; position: absolute; right: 3px; top: 4px; border: 0; background: transparent; color: var(--muted); width: 32px; height: 32px; border-radius: 6px; font-size: 22px; cursor: pointer; }
.mobile-close:hover { background: var(--hover); }

.new-note-btn, .sidebar-action {
    width: 100%; border: 0; background: transparent; cursor: pointer; border-radius: 6px;
    display: flex; align-items: center; gap: 8px; min-height: 32px; padding: 6px 8px; text-align: left;
    font-size: 13px; color: var(--muted); transition: background .12s ease, color .12s ease;
}
.new-note-btn { color: var(--text); font-weight: 600; margin-bottom: 12px; }
.new-note-btn span { color: var(--muted); font-size: 17px; line-height: 1; }
.new-note-btn:hover, .sidebar-action:hover { background: var(--hover); color: var(--text); }

.sidebar-section-title { color: var(--muted-2); font-size: 11px; font-weight: 600; padding: 7px 8px 5px; }
.category-nav { display: grid; gap: 1px; }
.category-link {
    display: grid; grid-template-columns: 22px minmax(0, 1fr) auto; align-items: center; min-height: 32px;
    gap: 6px; padding: 5px 8px; border-radius: 6px; color: var(--muted); font-size: 13px;
}
.category-link:hover { background: var(--hover); color: var(--text); }
.category-link.active { background: var(--active); color: var(--text); font-weight: 600; }
.cat-icon { width: 20px; text-align: center; font-size: 14px; }
.cat-label { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.cat-count { color: var(--muted-2); font-size: 11px; font-weight: 500; }
.sidebar-spacer { flex: 1; }
.sidebar-bottom { border-top: 1px solid var(--line); padding-top: 8px; }
.logout-form { margin: 0; }
.sidebar-bottom small { display: block; color: var(--muted-2); font-size: 10px; padding: 8px; }

.notes-column {
    height: 100vh;
    min-width: 0;
    background: var(--panel);
    border-right: 1px solid var(--line);
    display: grid;
    grid-template-rows: auto 1fr;
}
.notes-header { padding: 16px 14px 12px; border-bottom: 1px solid var(--line); background: var(--panel); }
.notes-heading-row { display: flex; align-items: center; gap: 9px; min-width: 0; margin-bottom: 12px; }
.notes-heading-row > div { min-width: 0; flex: 1; }
.notes-heading-row h1 { margin: 0; font-size: 15px; font-weight: 700; letter-spacing: -.015em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.notes-heading-row p { margin: 2px 0 0; color: var(--muted); font-size: 11px; }
.menu-btn, .header-add, .icon-btn {
    border: 0; background: transparent; color: var(--muted); cursor: pointer; border-radius: 6px; width: 30px; height: 30px;
    display: grid; place-items: center; font-size: 16px;
}
.menu-btn { display: none; }
.header-add:hover, .menu-btn:hover, .icon-btn:hover { background: var(--hover); color: var(--text); }
.search-box {
    display: flex; align-items: center; gap: 7px; height: 32px; padding: 0 9px; border: 1px solid var(--line);
    border-radius: 6px; background: var(--surface); color: var(--muted);
}
.search-box:focus-within { border-color: #c7c7c3; box-shadow: 0 0 0 2px rgba(55,53,47,.05); }
.search-box input { border: 0; outline: 0; background: transparent; color: var(--text); width: 100%; min-width: 0; font-size: 12px; }
.search-box input::placeholder { color: var(--muted-2); }

.notes-list { overflow: auto; min-height: 0; padding: 5px; scrollbar-width: thin; }
.note-row { display: block; padding: 10px 10px 9px; border-radius: 6px; margin-bottom: 1px; border: 1px solid transparent; }
.note-row:hover { background: var(--hover); }
.note-row.active { background: var(--active); }
.note-row-title { font-size: 13px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.note-row-preview { color: var(--muted); font-size: 11px; line-height: 1.45; height: 32px; margin-top: 4px; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.note-row-meta { color: var(--muted-2); font-size: 10px; margin-top: 5px; }
.empty-list { color: var(--muted); text-align: center; padding: 58px 22px; display: grid; justify-items: center; gap: 7px; }
.empty-list > span { font-size: 26px; }
.empty-list strong { color: var(--text); font-size: 13px; }
.empty-list p { margin: 0; font-size: 11px; line-height: 1.5; }
.text-btn { border: 0; background: transparent; color: var(--accent); cursor: pointer; padding: 5px 7px; font-size: 11px; font-weight: 650; }
.text-btn:hover { text-decoration: underline; }

.editor-column { min-width: 0; height: 100vh; background: var(--surface); position: relative; }
.editor-form { height: 100%; min-height: 0; display: grid; grid-template-rows: 46px minmax(0, 1fr) 34px; }
.editor-topbar { border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 0 14px; }
.category-control select {
    border: 0; background: transparent; color: var(--muted); outline: 0; cursor: pointer; padding: 6px 5px; border-radius: 5px; font-size: 11px; max-width: 210px;
}
.category-control select:hover { background: var(--hover); color: var(--text); }
.editor-actions { display: flex; align-items: center; gap: 2px; }
.save-state { color: var(--muted-2); font-size: 10px; margin-right: 5px; white-space: nowrap; }
.save-state.dirty { color: #b7791f; }
.save-state.saving { color: var(--accent); }
.plain-btn, .save-btn {
    border: 0; cursor: pointer; border-radius: 5px; min-height: 29px; padding: 5px 8px; background: transparent; font-size: 11px; color: var(--muted);
}
.plain-btn:hover { background: var(--hover); color: var(--text); }
.danger-text:hover { color: var(--danger); background: var(--danger-soft); }
.save-btn { color: #fff; background: var(--accent); font-weight: 650; padding-inline: 12px; }
.save-btn:hover { background: var(--accent-hover); }
.save-btn:disabled { opacity: .6; cursor: default; }

.document-wrap { width: min(850px, 100%); margin: 0 auto; min-height: 0; display: grid; grid-template-rows: auto minmax(0, 1fr); padding-top: clamp(32px, 6vh, 72px); }
.title-input { border: 0; outline: 0; background: transparent; color: var(--text); width: 100%; padding: 0 54px 12px; font-size: clamp(31px, 3.6vw, 44px); font-weight: 720; letter-spacing: -.035em; line-height: 1.15; }
.title-input::placeholder { color: var(--muted-2); }
.content-input { border: 0; outline: 0; background: transparent; color: var(--text); width: 100%; min-height: 0; resize: none; padding: 8px 54px 58px; font-size: 15px; line-height: 1.72; }
.content-input::placeholder { color: var(--muted-2); }
.editor-footer { border-top: 1px solid var(--line); display: flex; justify-content: flex-end; align-items: center; gap: 16px; color: var(--muted-2); font-size: 9px; padding: 0 16px; }
.editor-empty { height: 100%; display: grid; place-content: center; justify-items: center; text-align: center; color: var(--muted); padding: 24px; }
.empty-page-icon { font-size: 34px; color: var(--muted-2); }
.editor-empty h2 { color: var(--text); margin: 11px 0 5px; font-size: 18px; }
.editor-empty p { margin: 0 0 15px; font-size: 12px; }
.hidden-form { display: none; }

/* --- Generic controls --- */
.primary-btn, .secondary-btn, .qr-login-btn {
    border-radius: 6px; min-height: 38px; padding: 8px 13px; cursor: pointer; border: 1px solid transparent; font-weight: 650; font-size: 12px;
}
.primary-btn { color: #fff; background: var(--accent); }
.primary-btn:hover { background: var(--accent-hover); }
.secondary-btn { background: var(--surface); color: var(--text); border-color: var(--line); }
.secondary-btn:hover { background: var(--hover); }
.full { width: 100%; }

.toast { position: fixed; right: 18px; top: 17px; z-index: 90; background: var(--surface); border: 1px solid var(--line); border-radius: 7px; box-shadow: var(--shadow); padding: 9px 12px; font-size: 11px; font-weight: 650; }

/* --- New note dialog --- */
.note-dialog { border: 0; padding: 0; border-radius: 10px; background: transparent; width: min(520px, calc(100% - 28px)); color: var(--text); }
.note-dialog::backdrop { background: rgba(15,15,15,.32); backdrop-filter: blur(2px); }
.dialog-card { background: var(--surface); color: var(--text); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); padding: 19px; display: grid; gap: 14px; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; }
.dialog-head h2 { margin: 0; font-size: 18px; }
.dialog-card label, .auth-form label { display: grid; gap: 6px; color: var(--muted); font-size: 11px; font-weight: 600; }
.dialog-card input, .dialog-card textarea, .dialog-card select, .auth-form input {
    width: 100%; border: 1px solid var(--line); background: var(--surface); color: var(--text); border-radius: 6px; outline: 0; padding: 9px 10px; font-size: 13px;
}
.dialog-card textarea { min-height: 135px; resize: vertical; line-height: 1.5; }
.dialog-card input:focus, .dialog-card textarea:focus, .dialog-card select:focus, .auth-form input:focus { border-color: #b9b9b5; box-shadow: 0 0 0 2px rgba(55,53,47,.06); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 7px; }

/* --- Login / QR --- */
.auth-body { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: var(--sidebar); }
.auth-shell { width: 100%; max-width: 410px; }
.auth-card { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 28px; box-shadow: 0 2px 10px rgba(15,15,15,.05); }
.auth-brand { font-size: 13px; font-weight: 700; color: var(--text); margin-bottom: 28px; }
.auth-brand span { color: var(--muted-2); font-weight: 500; margin-left: 5px; }
.auth-card h1 { margin: 0 0 8px; font-size: 25px; line-height: 1.2; letter-spacing: -.025em; }
.auth-copy { color: var(--muted); margin: 0 0 22px; font-size: 12px; line-height: 1.55; }
.auth-form { display: grid; gap: 14px; }
.auth-form label small { color: var(--muted-2); font-size: 9px; font-weight: 500; line-height: 1.4; }
.alert { padding: 9px 10px; border-radius: 6px; margin-bottom: 13px; font-size: 11px; }
.alert.error { background: var(--danger-soft); color: var(--danger); border: 1px solid color-mix(in srgb, var(--danger) 18%, transparent); }
.auth-divider { display: flex; align-items: center; gap: 10px; margin: 17px 0 12px; color: var(--muted-2); font-size: 10px; }
.auth-divider::before, .auth-divider::after { content: ""; height: 1px; background: var(--line); flex: 1; }
.qr-login-btn { background: var(--surface); border-color: var(--line); color: var(--text); }
.qr-login-btn:hover { background: var(--hover); }
.qr-symbol { font-size: 15px; margin-right: 6px; }
.qr-login-panel { margin-top: 13px; padding: 13px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); }
.qr-panel-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.qr-panel-head strong { display: block; font-size: 12px; }
.qr-panel-head small { display: block; color: var(--muted); font-size: 10px; line-height: 1.45; margin-top: 3px; }
.qr-code-wrap { width: 230px; height: 230px; margin: 16px auto 10px; display: grid; place-items: center; background: #fff; border: 1px solid #ececea; border-radius: 7px; overflow: hidden; }
#qrCode canvas { display: block; width: 230px; height: 230px; }
.qr-status { text-align: center; font-size: 11px; font-weight: 650; }
.qr-countdown { min-height: 18px; text-align: center; color: var(--muted); font-size: 10px; margin: 4px 0 9px; }
.qr-approval-label { color: var(--muted-2); font-size: 9px; font-weight: 700; letter-spacing: .08em; margin-bottom: 10px; }
.device-card { display: flex; align-items: center; gap: 11px; border: 1px solid var(--line); background: var(--panel); border-radius: 7px; padding: 12px; margin: 17px 0; }
.device-icon { font-size: 22px; }
.device-card strong, .device-card small { display: block; }
.device-card strong { font-size: 12px; }
.device-card small { color: var(--muted); font-size: 10px; margin-top: 2px; }
.approve-actions { display: grid; grid-template-columns: 1fr 1.3fr; gap: 8px; }
.security-note { color: var(--muted-2); font-size: 9px; text-align: center; line-height: 1.5; margin: 13px 0 0; }

@media (max-width: 1050px) {
    .notion-shell { grid-template-columns: 210px 285px minmax(0, 1fr); }
    .title-input, .content-input { padding-left: 38px; padding-right: 38px; }
}

@media (max-width: 820px) {
    .notion-shell { grid-template-columns: 300px minmax(0, 1fr); }
    .sidebar { position: fixed; left: 0; top: 0; z-index: 100; width: 245px; transform: translateX(-102%); transition: transform .16s ease; box-shadow: var(--shadow); }
    .sidebar.open { transform: translateX(0); }
    .mobile-close, .menu-btn { display: grid; }
    .notes-column { grid-column: 1; }
    .editor-column { grid-column: 2; }
}

@media (max-width: 620px) {
    .app-body { overflow: auto; }
    .notion-shell { display: block; min-height: 100vh; }
    .notes-column { height: auto; min-height: 280px; max-height: 42vh; border-right: 0; border-bottom: 1px solid var(--line); }
    .notes-list { max-height: calc(42vh - 100px); }
    .editor-column { height: 58vh; min-height: 500px; }
    .editor-form { min-height: 500px; grid-template-rows: auto minmax(0, 1fr) auto; }
    .editor-topbar { min-height: 48px; padding: 6px 9px; flex-wrap: wrap; }
    .category-control { width: 100%; order: 2; }
    .category-control select { max-width: 100%; width: 100%; }
    .editor-actions { margin-left: auto; }
    .save-state { display: none; }
    .plain-btn, .save-btn { padding-inline: 7px; }
    .document-wrap { padding-top: 28px; }
    .title-input { font-size: 30px; padding: 0 20px 10px; }
    .content-input { padding: 7px 20px 42px; font-size: 14px; }
    .editor-footer { padding: 7px 10px; min-height: 38px; height: auto; flex-wrap: wrap; gap: 8px 14px; justify-content: flex-start; }
    .auth-card { padding: 22px; }
    .qr-code-wrap { width: 220px; height: 220px; }
    #qrCode canvas { width: 220px; height: 220px; }
}
