:root {
    --bg: #eceef1;
    --surface: #ffffff;
    --surface-2: #f4f5f7;
    --hover: #f0f1f4;
    --selected: #eef0f3;
    --line: #e7e9ed;
    --ink: #1f2023;
    --ink-2: #5d6168;
    --ink-3: #9a9ea6;
    --accent: #ffdb4d;
    --accent-hover: #ffd21f;
    --accent-soft: #fff2bf;
    --link: #1a6dd8;
    --danger: #e5484d;
    --brand: #ff0000;
    --radius: 16px;
    --rail-w: 60px;
    --sidebar-w: 300px;
    --shadow: 0 12px 40px rgba(20, 24, 31, .16);
    color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 20px; height: 20px; flex: none; }
a { color: var(--link); }
[hidden], .hidden { display: none !important; }
.muted { color: var(--ink-2); }
.small { font-size: 12.5px; }

.spinner {
    width: 28px; height: 28px; border-radius: 50%;
    border: 3px solid rgba(0, 0, 0, .08); border-top-color: var(--ink-2);
    animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.boot { position: fixed; inset: 0; display: grid; place-items: center; }

/* Buttons */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    height: 36px; padding: 0 16px; border-radius: 10px; border: 0;
    background: var(--surface-2); font-weight: 500; transition: background .15s;
}
.btn:hover { background: var(--hover); }
.btn svg { width: 18px; height: 18px; }
.btn-primary { background: var(--accent); color: #1b1b1b; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #d63c41; }
.btn-block { width: 100%; }
.btn-link { background: none; color: var(--ink-2); height: auto; padding: 8px; }
.btn-link:hover { background: none; color: var(--ink); text-decoration: underline; }

.icon-btn {
    display: inline-grid; place-items: center; width: 32px; height: 32px;
    border: 0; border-radius: 8px; background: none; color: var(--ink-2);
}
.icon-btn:hover { background: var(--hover); color: var(--ink); }
.icon-btn svg { width: 18px; height: 18px; }

.input {
    width: 100%; height: 42px; padding: 0 14px; border-radius: 10px;
    border: 1px solid var(--line); background: var(--surface); outline: none;
}
.input:focus { border-color: #c9ccd2; box-shadow: 0 0 0 3px rgba(255, 219, 77, .45); }

/* Login */
.login {
    min-height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center;
    padding: 32px 16px;
}
.login-card {
    width: 100%; max-width: 380px; background: var(--surface); border-radius: 20px;
    padding: 32px 28px 24px; box-shadow: 0 10px 30px rgba(0, 0, 0, .06); text-align: center;
}
.login-brand h1 { margin: 12px 0 4px; font-size: 22px; font-weight: 650; }
.login-brand p { margin: 0 0 22px; }
.login-logo { width: 44px; height: 48px; }
.login-checking { display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 24px 0; }
.login-profile { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.login-avatar {
    width: 64px; height: 64px; border-radius: 50%; display: grid; place-items: center;
    background: var(--accent-soft); font-size: 22px; font-weight: 600; margin-bottom: 8px;
}
.login-profile-name { font-size: 17px; font-weight: 600; }
.login-profile-phone { margin-bottom: 16px; }
.login-tabs { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; margin-bottom: 16px; }
.login-tab { flex: 1; height: 32px; border: 0; border-radius: 8px; background: none; color: var(--ink-2); font-weight: 500; }
.login-tab.active { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(0, 0, 0, .08); }
.login-panel { display: flex; flex-direction: column; gap: 12px; align-items: center; }
.login-panel p { margin: 0; }
.qr-box {
    width: 232px; height: 232px; display: grid; place-items: center;
    border: 1px solid var(--line); border-radius: 14px; padding: 12px;
}
.qr-box img, .qr-box canvas, .qr-box svg { width: 100% !important; height: auto !important; display: block; }
.login-status { min-height: 18px; margin: 14px 0 0; }
.login-error { margin: 10px 0 0; color: var(--danger); font-size: 13px; }
.login-foot { margin-top: 18px; }

/* App shell */
.app { display: flex; height: 100vh; height: 100dvh; padding: 8px 8px 8px 0; }
.rail {
    width: var(--rail-w); flex: none; display: flex; flex-direction: column; align-items: center;
    padding: 6px 0 4px;
}
.rail-logo { display: grid; place-items: center; width: 40px; height: 40px; margin-bottom: 14px; }
.rail-logo img { width: 26px; height: 28px; }
.rail-services { display: flex; flex-direction: column; gap: 4px; align-items: center; }
.rail-item {
    position: relative; display: grid; place-items: center; width: 40px; height: 40px;
    border-radius: 12px; color: #4a4e55; text-decoration: none; transition: background .15s, color .15s;
}
.rail-item svg { width: 21px; height: 21px; }
.rail-item:hover { background: rgba(0, 0, 0, .06); color: var(--ink); }
.rail-item.active { background: var(--surface); color: var(--brand); box-shadow: 0 1px 4px rgba(0, 0, 0, .06); }
.rail-item .tip {
    position: absolute; left: 48px; top: 50%; transform: translateY(-50%); white-space: nowrap;
    background: #2a2c31; color: #fff; font-size: 12px; padding: 4px 9px; border-radius: 6px;
    opacity: 0; pointer-events: none; transition: opacity .12s; z-index: 30;
}
.rail-item:hover .tip { opacity: 1; }
.rail-bottom { margin-top: auto; position: relative; display: flex; flex-direction: column; align-items: center; gap: 6px; }
.rail-avatar, .user-avatar {
    width: 34px; height: 34px; flex: none; border-radius: 50%; border: 0; padding: 0; overflow: hidden;
    display: grid; place-items: center; background: var(--accent-soft);
    font-size: 13px; font-weight: 600; color: #5a4700;
}
.rail-avatar img, .user-avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.user-avatar { width: 44px; height: 44px; font-size: 16px; }
.user-menu-head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.user-menu-who { min-width: 0; }
.user-menu {
    position: absolute; left: 46px; bottom: 0; width: 230px; background: var(--surface);
    border-radius: 14px; box-shadow: var(--shadow); padding: 12px; z-index: 40;
}
.user-menu-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-menu-sub { margin: 0; }
.user-menu-item {
    display: flex; align-items: center; gap: 10px; width: 100%; padding: 8px; border: 0;
    border-radius: 8px; background: none; text-align: left;
}
.user-menu-item:hover { background: var(--hover); }
.user-menu-item svg { width: 18px; height: 18px; color: var(--ink-2); }

.workspace {
    flex: 1; min-width: 0; display: flex; background: var(--surface); border-radius: var(--radius);
    overflow: hidden; box-shadow: 0 1px 2px rgba(0, 0, 0, .04);
}

/* Sidebar */
.sidebar {
    width: var(--sidebar-w); flex: none; display: flex; flex-direction: column;
    border-right: 1px solid var(--line); padding: 16px 12px 8px; min-height: 0;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 0 4px 12px; }
.sidebar-head h2 { margin: 0; font-size: 20px; font-weight: 650; }
.search {
    display: flex; align-items: center; gap: 8px; height: 36px; padding: 0 10px; margin-bottom: 10px;
    background: var(--surface-2); border-radius: 10px; color: var(--ink-3);
}
.search svg { width: 16px; height: 16px; }
.search-input { flex: 1; min-width: 0; border: 0; outline: none; background: none; }
.btn-new { width: 100%; height: 38px; margin-bottom: 12px; }
.note-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; min-height: 0; }
.note-item {
    display: flex; gap: 10px; align-items: center; padding: 9px 10px; border-radius: 10px;
    cursor: pointer; margin-bottom: 2px; user-select: none;
}
.note-item:hover { background: var(--hover); }
.note-item.active { background: var(--selected); }
.note-item-main { flex: 1; min-width: 0; }
.note-item-title {
    display: flex; align-items: center; gap: 4px; font-weight: 600; font-size: 13.5px;
    overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
}
.note-item-title span { overflow: hidden; text-overflow: ellipsis; }
.note-item-title svg { width: 13px; height: 13px; color: #e0a800; }
.note-item-meta { display: flex; gap: 6px; font-size: 12.5px; color: var(--ink-2); white-space: nowrap; overflow: hidden; }
.note-item-date { color: var(--ink-3); flex: none; }
.note-item-snippet { overflow: hidden; text-overflow: ellipsis; }
.note-item-thumb { width: 38px; height: 38px; border-radius: 6px; object-fit: cover; flex: none; background: var(--surface-2); }
.list-empty { padding: 16px 8px; text-align: center; }

/* Editor */
.editor { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
.toolbar { display: flex; align-items: center; gap: 8px; padding: 12px 16px 8px 20px; }
.toolbar-group { display: flex; align-items: center; gap: 2px; }
.toolbar-right { margin-left: auto; }
.tool {
    display: inline-grid; place-items: center; width: 32px; height: 32px; border: 0; border-radius: 8px;
    background: none; color: #3f4248; font-size: 15px; line-height: 1;
}
.tool svg { width: 19px; height: 19px; }
.tool:hover { background: var(--hover); }
.tool.active { background: var(--accent-soft); color: #1b1b1b; }
.tool:disabled { opacity: .35; cursor: default; background: none; }
#pin-button.active { background: none; color: #e0a800; }
.save-state { margin-left: 8px; white-space: nowrap; }

.editor-scroll { flex: 1; overflow-y: auto; min-height: 0; position: relative; }
.editor-inner { padding: 8px 32px 40px 36px; max-width: 980px; }
.gallery { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.gallery:empty { display: none; }
.thumb {
    position: relative; width: 132px; height: 132px; border-radius: 10px; overflow: hidden;
    background: var(--surface-2); cursor: zoom-in; flex: none;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb-del {
    position: absolute; top: 6px; right: 6px; width: 26px; height: 26px; border-radius: 50%; border: 0;
    display: grid; place-items: center; background: rgba(20, 22, 26, .65); color: #fff; opacity: 0; transition: opacity .15s;
}
.thumb-del svg { width: 15px; height: 15px; }
.thumb:hover .thumb-del { opacity: 1; }
.thumb.uploading { cursor: default; display: grid; place-items: center; }
.thumb.uploading img { position: absolute; inset: 0; opacity: .45; }
.thumb.uploading .spinner { position: relative; }

.note-title {
    display: block; width: 100%; border: 0; outline: none; resize: none; overflow: hidden; padding: 0;
    font-size: 20px; font-weight: 650; line-height: 1.35; margin: 4px 0 10px; background: none;
}
.note-title::placeholder { color: var(--ink-3); }
.note-body { outline: none; min-height: 240px; font-size: 14.5px; line-height: 1.6; word-wrap: break-word; }
.note-body:empty::before { content: attr(data-placeholder); color: var(--ink-3); pointer-events: none; }
.note-body ul, .note-body ol { margin: 4px 0; padding-left: 22px; }
.note-body p { margin: 0 0 6px; }
.note-body a { color: var(--link); cursor: pointer; }

.editor-empty {
    position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 10px; color: var(--ink-3);
}
.editor-empty svg { width: 48px; height: 48px; opacity: .5; }
.editor.no-note .toolbar .tool, .editor.no-note .save-state { visibility: hidden; }

.drop-overlay {
    position: absolute; inset: 10px; border: 2px dashed #e0b400; border-radius: 14px;
    background: rgba(255, 242, 191, .75); display: grid; place-items: center;
    font-size: 16px; font-weight: 600; z-index: 20; pointer-events: none;
}

.footer {
    display: flex; justify-content: space-between; align-items: center; padding: 10px 20px 10px 36px;
    font-size: 12px; color: var(--ink-3);
}
.footer a { color: var(--ink-3); text-decoration: none; }
.footer a:hover { color: var(--ink); }
.footer-right { display: flex; align-items: center; gap: 12px; }
.lang-btn { border: 0; background: none; color: var(--ink-3); font-size: 12px; padding: 2px 4px; border-radius: 4px; text-transform: uppercase; }
.lang-btn:hover { color: var(--ink); background: var(--hover); }

/* Link popover */
.link-pop {
    position: fixed; z-index: 50; display: flex; align-items: center; gap: 6px; max-width: 360px;
    background: var(--surface); border-radius: 10px; box-shadow: var(--shadow); padding: 6px 6px 6px 12px;
}
.link-pop a { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 13px; }
.link-pop-open { display: grid; place-items: center; width: 28px; height: 28px; border-radius: 6px; flex: none; color: var(--ink-2) !important; }
.link-pop-open:hover { background: var(--hover); }
.link-pop-open svg { width: 16px; height: 16px; }

/* Lightbox */
.lightbox {
    position: fixed; inset: 0; z-index: 60; background: rgba(12, 13, 15, .92);
    display: flex; align-items: center; justify-content: center;
}
.lightbox img { max-width: calc(100vw - 140px); max-height: calc(100vh - 110px); object-fit: contain; border-radius: 4px; }
.lightbox-top {
    position: absolute; top: 0; left: 0; right: 0; display: flex; justify-content: space-between; align-items: center;
    padding: 12px 16px; color: #d6d8dc;
}
.lightbox-actions { display: flex; gap: 4px; }
.lb-btn, .lb-nav {
    display: grid; place-items: center; width: 40px; height: 40px; border-radius: 10px; border: 0;
    background: rgba(255, 255, 255, .08); color: #fff;
}
.lb-btn:hover, .lb-nav:hover { background: rgba(255, 255, 255, .18); }
.lb-nav { position: absolute; top: 50%; transform: translateY(-50%); width: 48px; height: 48px; border-radius: 50%; }
.lb-prev { left: 20px; }
.lb-next { right: 20px; }

/* Dialog + toast */
.dialog-backdrop { position: fixed; inset: 0; z-index: 70; background: rgba(15, 17, 20, .35); display: grid; place-items: center; padding: 16px; }
.dialog { width: 100%; max-width: 360px; background: var(--surface); border-radius: 16px; padding: 22px; box-shadow: var(--shadow); }
.dialog p { margin: 0 0 20px; font-size: 15px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.toast {
    position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 80;
    background: #25272c; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 13.5px;
    box-shadow: var(--shadow); max-width: calc(100vw - 32px);
}

.mobile-only { display: none; }

@media (max-width: 1100px) {
    :root { --sidebar-w: 260px; }
    .thumb { width: 110px; height: 110px; }
}

@media (max-width: 760px) {
    .app { padding: 0; }
    .rail { display: none; }
    .workspace { border-radius: 0; }
    .sidebar { width: 100%; border-right: 0; }
    .editor { display: none; }
    .app.show-editor .sidebar { display: none; }
    .app.show-editor .editor { display: flex; }
    .mobile-only { display: inline-grid; }
    .toolbar { padding: 8px 8px 6px; gap: 2px; overflow-x: auto; }
    .save-state { display: none; }
    .editor-inner { padding: 6px 16px 32px; }
    .footer { display: none; }
    .thumb { width: calc((100vw - 48px) / 3); height: calc((100vw - 48px) / 3); }
    .thumb-del { opacity: 1; }
    .lightbox img { max-width: 100vw; max-height: calc(100vh - 140px); }
    .lb-nav { top: auto; bottom: 20px; transform: none; }
}
