:root {
    --bg: #f4f5f7;
    --panel: #ffffff;
    --border: #e3e6ea;
    --text: #1f2328;
    --muted: #8a929b;
    --primary: #2563eb;
    --primary-d: #1d4ed8;
    --danger: #dc2626;
    --hover: #f0f4ff;
    --active: #e0eaff;
    --sidebar-w: 290px;
    --shadow: 0 1px 3px rgba(0,0,0,.08);
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    height: 100%;
    overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
    color: var(--text);
    background: var(--bg);
    -webkit-tap-highlight-color: transparent;
}

/* ---------- Layout ---------- */
.app {
    display: flex;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
}

.sidebar {
    width: var(--sidebar-w);
    flex: 0 0 var(--sidebar-w);
    background: var(--panel);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.sidebar-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
}

.brand { font-weight: 700; font-size: 18px; letter-spacing: .3px; }

.note-list {
    list-style: none;
    margin: 0;
    padding: 6px;
    overflow-y: auto;
    flex: 1 1 auto;
    min-height: 0;
}

.note-item {
    padding: 10px 12px;
    border-radius: 8px;
    cursor: pointer;
    margin-bottom: 2px;
}
.note-item:hover { background: var(--hover); }
.note-item.active { background: var(--active); }

.note-item .ni-title {
    font-size: 14px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.note-item .ni-date {
    font-size: 12px;
    color: var(--muted);
    margin-top: 3px;
}

.note-list .empty {
    color: var(--muted);
    font-size: 14px;
    text-align: center;
    padding: 24px 12px;
}

.sidebar-foot {
    padding: 12px 16px;
    border-top: 1px solid var(--border);
}
.logout-link { color: var(--muted); font-size: 13px; text-decoration: none; }
.logout-link:hover { color: var(--primary); }

/* ---------- Content ---------- */
.content {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-width: 0;
    background: var(--bg);
}

.topbar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    background: var(--panel);
    border-bottom: 1px solid var(--border);
}

.note-title {
    flex: 1 1 auto;
    min-width: 0;
    border: 1px solid transparent;
    background: transparent;
    font-size: 16px;
    font-weight: 600;
    padding: 8px 10px;
    border-radius: 8px;
    color: var(--text);
}
.note-title:hover:not(:disabled) { background: var(--bg); }
.note-title:focus { outline: none; border-color: var(--primary); background: #fff; }

.actions { display: flex; align-items: center; gap: 8px; }

.save-state { font-size: 12px; color: var(--muted); white-space: nowrap; }

.editor {
    flex: 1 1 auto;
    width: 100%;
    border: none;
    resize: none;
    padding: 22px clamp(16px, 5vw, 60px);
    font-size: 15px;
    line-height: 1.7;
    background: var(--bg);
    color: var(--text);
    font-family: "SFMono-Regular", "Consolas", "Liberation Mono", Menlo, monospace;
}
.editor:focus { outline: none; }
.editor:disabled { background: var(--bg); color: var(--muted); }

.meta-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 4px 16px;
    border-top: 1px solid var(--border);
    background: var(--panel);
    min-height: 28px;
}
.meta {
    font-size: 12px;
    color: var(--muted);
}
.btn-link {
    border: none;
    background: transparent;
    color: var(--primary);
    font-size: 12px;
    cursor: pointer;
    padding: 4px 6px;
    border-radius: 6px;
    white-space: nowrap;
}
.btn-link:hover { background: var(--hover); }

/* ---------- Buttons ---------- */
.btn {
    border: 1px solid var(--border);
    background: #fff;
    color: var(--text);
    padding: 8px 14px;
    border-radius: 8px;
    font-size: 14px;
    cursor: pointer;
    white-space: nowrap;
}
.btn:hover:not(:disabled) { background: var(--hover); }
.btn:disabled { opacity: .45; cursor: default; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-d); }
.btn-danger { color: var(--danger); border-color: #f3c7c7; }
.btn-danger:hover:not(:disabled) { background: #fdeaea; }

.icon-btn {
    border: none;
    background: transparent;
    font-size: 22px;
    line-height: 1;
    width: 38px;
    height: 38px;
    border-radius: 8px;
    cursor: pointer;
    color: var(--text);
}
.icon-btn:hover { background: var(--hover); }

.hamburger { display: none; }

.overlay {
    display: none;
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(0,0,0,.35);
    z-index: 40;
}

/* ---------- Login ---------- */
.login-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    min-height: 100dvh;
    background: linear-gradient(135deg, #eef2ff, #f4f5f7);
}
.login-box {
    background: var(--panel);
    padding: 32px 28px;
    border-radius: 14px;
    box-shadow: 0 10px 40px rgba(0,0,0,.10);
    width: 320px;
    max-width: calc(100vw - 32px);
    text-align: center;
}
.login-box h1 { margin: 0 0 4px; font-size: 26px; }
.login-sub { margin: 0 0 20px; color: var(--muted); font-size: 14px; }
.login-box input {
    width: 100%;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: 9px;
    font-size: 16px;
    margin-bottom: 12px;
}
.login-box input:focus { outline: none; border-color: var(--primary); }
.login-box button {
    width: 100%;
    padding: 12px;
    border: none;
    border-radius: 9px;
    background: var(--primary);
    color: #fff;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
}
.login-box button:hover { background: var(--primary-d); }
.login-error {
    background: #fdeaea;
    color: var(--danger);
    padding: 9px;
    border-radius: 8px;
    font-size: 13px;
    margin-bottom: 14px;
}

/* ---------- File đính kèm ---------- */
.attachments {
    border-top: 1px solid var(--border);
    background: var(--panel);
    padding: 10px 16px;
    max-height: 180px;
    overflow-y: auto;
    flex: 0 0 auto;
}
.att-head {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 6px;
}
.att-title { font-size: 13px; font-weight: 600; color: var(--muted); }
.att-add-btn { font-size: 12px; padding: 4px 10px; cursor: pointer; }
.att-status { font-size: 12px; color: var(--primary); }

.att-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}
.att-item {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 5px 10px;
    max-width: 100%;
}
.att-name {
    font-size: 13px;
    color: var(--primary);
    text-decoration: none;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 240px;
}
.att-name:hover { text-decoration: underline; }
.att-size { font-size: 11px; color: var(--muted); flex: 0 0 auto; }
.att-del {
    border: none;
    background: transparent;
    color: var(--muted);
    font-size: 12px;
    cursor: pointer;
    padding: 2px 4px;
    border-radius: 4px;
    flex: 0 0 auto;
}
.att-del:hover { color: var(--danger); background: #fdeaea; }

/* ---------- Share button ---------- */
.btn-share { color: var(--primary); border-color: #c7d9ff; }
.btn-share:hover:not(:disabled) { background: #eef3ff; }
.btn-share-active { background: #eef3ff; border-color: var(--primary); }

/* ---------- Share modal ---------- */
.modal-backdrop {
    display: none;
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(0,0,0,.40);
    z-index: 100;
}
.modal-backdrop.open {
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal {
    background: var(--panel);
    border-radius: 14px;
    width: 420px;
    max-width: calc(100vw - 32px);
    box-shadow: 0 16px 48px rgba(0,0,0,.18);
    overflow: hidden;
}
.modal-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}
.modal-close { font-size: 16px; }

.share-info {
    margin: 0 0 14px;
    font-size: 14px;
    color: var(--muted);
    line-height: 1.5;
}

#shareBody {
    padding: 20px;
}

.share-url-row {
    display: flex;
    gap: 8px;
    margin-bottom: 14px;
}
.share-url-input {
    flex: 1 1 auto;
    min-width: 0;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 9px 12px;
    font-size: 13px;
    color: var(--muted);
    background: var(--bg);
}
.share-url-input:focus { outline: none; }

.share-actions { display: flex; gap: 8px; }

/* Chọn quyền chia sẻ */
.mode-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 14px;
}
.mode-opt {
    display: flex;
    align-items: flex-start;
    gap: 9px;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 9px;
    cursor: pointer;
    font-size: 13px;
    line-height: 1.45;
}
.mode-opt:hover { background: var(--hover); }
.mode-opt input { margin-top: 2px; flex: 0 0 auto; }
.mode-opt:has(input:checked) { border-color: var(--primary); background: #eef3ff; }

/* Danh sách lịch sử phiên bản */
.hist-list {
    list-style: none;
    margin: 0;
    padding: 0;
    max-height: 300px;
    overflow-y: auto;
}
.hist-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 9px 4px;
    border-bottom: 1px solid var(--border);
}
.hist-item:last-child { border-bottom: none; }
.hist-info { font-size: 13px; color: var(--text); }
.hist-item .btn { font-size: 12px; padding: 5px 11px; flex: 0 0 auto; }

.btn-danger-soft { color: var(--danger); border-color: #f3c7c7; font-size: 13px; padding: 8px 14px; }
.btn-danger-soft:hover { background: #fdeaea; }

/* ---------- Note list shared badge ---------- */
.ni-row {
    display: flex;
    align-items: center;
    gap: 6px;
    min-width: 0;
}
.ni-row .ni-title { flex: 1 1 auto; min-width: 0; }
.ni-shared {
    font-size: 11px;
    color: var(--primary);
    flex: 0 0 auto;
    opacity: .75;
}

/* ---------- Share page (share.php) ---------- */
.share-page {
    min-height: 100vh;
    min-height: 100dvh;
    background: var(--bg);
    display: flex;
    flex-direction: column;
}
.share-header {
    background: var(--panel);
    border-bottom: 1px solid var(--border);
    padding: 14px clamp(16px, 6vw, 80px);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}
.share-header .brand { font-size: 16px; font-weight: 700; color: var(--muted); text-decoration: none; }
.share-header .sh-title { font-size: 17px; font-weight: 700; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.share-header .btn { flex: 0 0 auto; }

.share-content {
    flex: 1 1 auto;
    padding: 28px clamp(16px, 6vw, 80px);
    white-space: pre-wrap;
    word-break: break-word;
    font-size: 15px;
    line-height: 1.75;
    font-family: "SFMono-Regular", "Consolas", "Liberation Mono", Menlo, monospace;
    max-width: 900px;
}

.share-footer {
    text-align: center;
    padding: 16px;
    font-size: 12px;
    color: var(--muted);
    border-top: 1px solid var(--border);
}
.share-footer a { color: var(--muted); text-decoration: none; }
.share-footer a:hover { color: var(--primary); }

/* Editor cho khách khi link có quyền edit */
.share-editor {
    flex: 1 1 auto;
    width: auto;
    margin: 20px clamp(16px, 6vw, 80px);
    max-width: 900px;
    min-height: 55vh;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 16px;
    font-size: 15px;
    line-height: 1.75;
    font-family: "SFMono-Regular", "Consolas", "Liberation Mono", Menlo, monospace;
    color: var(--text);
    background: var(--panel);
    resize: vertical;
}
.share-editor:focus { outline: none; border-color: var(--primary); }

.share-attachments {
    padding: 0 clamp(16px, 6vw, 80px) 28px;
    max-width: 900px;
}
.share-attachments .att-title {
    font-size: 13px;
    font-weight: 600;
    color: var(--muted);
    margin-bottom: 8px;
}

.share-not-found {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--muted);
    gap: 8px;
}
.share-not-found h2 { margin: 0; font-size: 20px; }
.share-not-found p  { margin: 0; font-size: 14px; }

/* ---------- Mobile ---------- */
@media (max-width: 760px) {
    .hamburger { display: inline-flex; align-items: center; justify-content: center; }

    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        z-index: 50;
        width: 82%;
        max-width: 320px;
        flex-basis: auto;
        transform: translateX(-100%);
        transition: transform .25s ease;
        box-shadow: var(--shadow);
    }
    /* Mặc định trên mobile: hiện sidebar (xem hết note) khi mới vào, app.js sẽ điều khiển */
    .app.sidebar-open .sidebar { transform: translateX(0); }
    .app.sidebar-open .overlay { display: block; }

    .note-title { font-size: 15px; }
    .editor { padding: 18px 16px; }
    .btn { padding: 8px 11px; }

    /* Topbar 2 dòng: dòng 1 = menu + tên note, dòng 2 = các nút chức năng */
    .topbar {
        flex-wrap: wrap;
        row-gap: 8px;
        padding: 8px 10px;
    }
    .actions {
        flex: 1 0 100%;          /* chiếm nguyên dòng thứ 2 */
        justify-content: flex-end;
    }
    .actions .save-state {
        margin-right: auto;      /* trạng thái lưu nằm bên trái dòng nút */
    }
}
