: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;
    --star: #e0a800;
    --radius: 16px;
    --rail-w: 60px;
    --sidebar-w: 240px;
    --list-w: 340px;
    --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; white-space: nowrap;
}
.btn:hover { background: var(--hover); }
.btn:disabled { opacity: .55; cursor: default; }
.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; }
.btn-ghost { background: none; color: var(--ink-2); padding: 0 10px; }
.btn-ghost:hover { background: var(--hover); color: var(--ink); }

.icon-btn {
    display: inline-grid; place-items: center; width: 32px; height: 32px; flex: none;
    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; }
.icon-btn-sm { width: 26px; height: 26px; }
.icon-btn-sm svg { width: 16px; height: 16px; }
.icon-btn.star-on, .icon-btn.star-on:hover { color: var(--star); }

.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); }
.input.invalid { border-color: var(--danger); }

/* 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; display: flex; align-items: center; justify-content: center; gap: 12px; }

/* 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); position: relative;
}

/* Sidebar: views + groups */
.sidebar {
    width: var(--sidebar-w); flex: none; display: flex; flex-direction: column;
    border-right: 1px solid var(--line); padding: 16px 10px 8px; min-height: 0; background: var(--surface);
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 0 6px 12px; }
.sidebar-head h2 { margin: 0; font-size: 20px; font-weight: 650; }
.sidebar-head-actions { display: flex; align-items: center; gap: 2px; }
.btn-new { width: 100%; height: 38px; margin-bottom: 12px; }
.nav { display: flex; flex-direction: column; gap: 1px; }
.nav-groups { overflow-y: auto; flex: 1; min-height: 0; padding-bottom: 8px; }
.nav-item {
    position: relative; display: flex; align-items: center; gap: 10px; width: 100%; height: 36px; padding: 0 8px 0 10px;
    border: 0; border-radius: 10px; background: none; text-align: left; color: var(--ink);
}
.nav-item svg { width: 18px; height: 18px; color: var(--ink-2); }
.nav-item > span:first-of-type { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item:hover { background: var(--hover); }
.nav-item.active { background: var(--accent-soft); font-weight: 600; }
.nav-item.active svg { color: #5a4700; }
.nav-count { font-size: 12.5px; color: var(--ink-3); font-weight: 400; }
.nav-item .nav-more { display: none; margin-right: -4px; }
.nav-item:hover .nav-more, .nav-item.menu-open .nav-more { display: inline-grid; }
.nav-item:hover .nav-count, .nav-item.menu-open .nav-count { display: none; }
.nav-title {
    display: flex; align-items: center; justify-content: space-between; margin: 14px 0 4px;
    padding: 0 4px 0 10px; font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-3);
}
.nav-hint { padding: 4px 10px; font-size: 12.5px; color: var(--ink-3); }
.sidebar-backdrop { display: none; }

/* List */
.list-pane {
    width: var(--list-w); flex: none; display: flex; flex-direction: column; min-height: 0;
    border-right: 1px solid var(--line);
}
.list-head, .bulk-bar { display: flex; align-items: center; gap: 6px; padding: 14px 12px 8px; min-height: 58px; }
.search {
    flex: 1; display: flex; align-items: center; gap: 8px; height: 36px; padding: 0 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; color: var(--ink); }
.bulk-count { flex: 1; font-weight: 600; white-space: nowrap; }
.bulk-actions { display: flex; gap: 2px; }
.list-title { padding: 2px 22px 6px; font-size: 12.5px; color: var(--ink-3); }
.contact-list { overflow-y: auto; flex: 1; min-height: 0; padding: 0 8px 12px; }
.letter {
    position: sticky; top: 0; z-index: 1; background: var(--surface);
    padding: 8px 12px 4px; font-size: 12px; font-weight: 650; color: var(--ink-3);
}
.contact-item {
    display: flex; align-items: center; gap: 12px; padding: 7px 10px; border-radius: 10px;
    cursor: pointer; user-select: none;
}
.contact-item:hover { background: var(--hover); }
.contact-item.active { background: var(--selected); }
.contact-item.checked { background: var(--accent-soft); }
.contact-item-main { flex: 1; min-width: 0; }
.contact-item-name { font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.contact-item-sub { font-size: 12.5px; color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.contact-item-star { width: 15px; height: 15px; color: var(--star); }
.list-empty { padding: 24px 16px; text-align: center; }

.avatar {
    position: relative; width: 38px; height: 38px; flex: none; border-radius: 50%; display: grid; place-items: center;
    font-size: 14px; font-weight: 600; color: #fff; text-transform: uppercase; overflow: hidden;
}
.avatar-lg { width: 88px; height: 88px; font-size: 32px; }
.contact-item .avatar .tick {
    position: absolute; inset: 0; display: none; place-items: center; background: #3b3e44; color: #fff; border-radius: 50%;
}
.contact-item .avatar .tick svg { width: 20px; height: 20px; }
.contact-item.checked .avatar .tick { display: grid; background: #1f2023; }
.contact-item:not(.checked) .avatar:hover .tick { display: grid; background: rgba(31, 32, 35, .55); }

.check {
    display: inline-grid; place-items: center; width: 32px; height: 32px; border-radius: 8px; border: 0; background: none;
    color: var(--ink-2);
}
.check svg { width: 18px; height: 18px; padding: 1px; border: 1.6px solid currentColor; border-radius: 5px; color: transparent; }
.check:hover { background: var(--hover); }
.check.on svg { background: #1f2023; border-color: #1f2023; color: #fff; }
.check.partial svg { background: linear-gradient(#1f2023, #1f2023) center / 10px 2px no-repeat; border-color: #1f2023; }

/* Detail */
.detail { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
.detail-scroll { flex: 1; overflow-y: auto; min-height: 0; position: relative; }
.detail-inner { padding: 24px 36px 40px; max-width: 760px; }
.detail-empty {
    position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 10px; color: var(--ink-3);
}
.detail-empty svg { width: 48px; height: 48px; opacity: .5; }
.detail-top { display: flex; align-items: center; gap: 8px; margin: -8px -12px 10px; }
.detail-top .spacer { flex: 1; }
.detail-hero { display: flex; align-items: center; gap: 20px; margin-bottom: 24px; }
.detail-hero-main { min-width: 0; }
.detail-name { margin: 0; font-size: 24px; font-weight: 650; line-height: 1.25; word-wrap: break-word; }
.detail-sub { color: var(--ink-2); margin-top: 2px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.chip {
    display: inline-flex; align-items: center; gap: 4px; height: 28px; padding: 0 12px; border-radius: 14px;
    background: var(--surface-2); border: 1px solid transparent; font-size: 13px; color: var(--ink);
}
button.chip:hover { background: var(--hover); border-color: var(--line); }
.chip svg { width: 15px; height: 15px; }
.chip.on { background: var(--accent-soft); border-color: #f1d36a; font-weight: 550; }
.chip-add { color: var(--ink-2); background: none; border: 1px dashed #cfd2d8; }

.section { margin-top: 22px; }
.section-title { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-3); margin-bottom: 6px; }
.row {
    display: flex; align-items: center; gap: 12px; min-height: 48px; padding: 4px 6px 4px 12px; margin: 0 -12px;
    border-radius: 10px;
}
.row:hover { background: var(--surface-2); }
.row > svg { color: var(--ink-3); width: 19px; height: 19px; }
.row-main { flex: 1; min-width: 0; }
.row-value { font-size: 15px; overflow-wrap: anywhere; }
.row-value a { color: var(--ink); text-decoration: none; }
.row-value a:hover { color: var(--link); text-decoration: underline; }
.row-label { font-size: 12.5px; color: var(--ink-3); }
.row-actions { display: flex; gap: 2px; opacity: 0; transition: opacity .12s; }
.row:hover .row-actions, .row:focus-within .row-actions { opacity: 1; }

/* Form */
.form-head { display: flex; align-items: center; gap: 8px; margin: -8px -12px 16px; }
.form-head h2 { margin: 0 0 0 4px; font-size: 20px; font-weight: 650; flex: 1; }
.field { margin-bottom: 14px; }
.field label, .field .label { display: block; font-size: 12.5px; color: var(--ink-2); margin-bottom: 5px; font-weight: 500; }
.phone-row { display: flex; gap: 6px; align-items: center; margin-bottom: 8px; }
.phone-row .input { flex: 1; }
.fields-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.form-error { color: var(--danger); font-size: 13px; margin: 4px 0 14px; }
.form-actions {
    position: sticky; bottom: 0; display: flex; gap: 8px; padding: 14px 0 4px; margin-top: 18px;
    background: linear-gradient(transparent, var(--surface) 30%);
}
.toggle-more { margin: 4px 0 12px -8px; }
.toggle-more svg { transition: transform .15s; }
.toggle-more.open svg { transform: rotate(180deg); }

.footer {
    display: flex; justify-content: space-between; align-items: center; gap: 16px; padding: 10px 20px 10px 36px;
    font-size: 12px; color: var(--ink-3);
}
.footer > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--ink-3); }
.footer a { color: var(--ink-3); text-decoration: none; white-space: nowrap; }
.footer a:hover { color: var(--ink); }
.footer-right { display: flex; align-items: center; gap: 12px; white-space: nowrap; }
.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, .lang-btn[aria-expanded="true"] { color: var(--ink); background: var(--hover); }
.lang-btn-mobile { display: none; }
.lang-menu, .pop-menu {
    position: fixed; z-index: 90; width: 210px; max-height: min(360px, calc(100vh - 24px));
    overflow: auto; background: var(--surface); border-radius: 12px; box-shadow: var(--shadow); padding: 6px;
}
.pop-menu { width: 240px; }
.lang-option, .pop-item {
    display: flex; align-items: center; gap: 10px; width: 100%; text-align: left; border: 0; background: none;
    padding: 8px 10px; border-radius: 8px; font-size: 14px; color: var(--ink);
}
.pop-item svg { width: 17px; height: 17px; color: var(--ink-2); }
.pop-item span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pop-item.danger, .pop-item.danger svg { color: var(--danger); }
.lang-option:hover, .pop-item:hover { background: var(--hover); }
.lang-option.active { background: var(--accent-soft); font-weight: 600; }
.pop-sep { height: 1px; background: var(--line); margin: 4px 6px; }
.pop-empty { padding: 8px 10px; color: var(--ink-3); font-size: 13px; }

/* 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 16px; font-size: 15px; }
.dialog-input { margin-bottom: 18px; }
.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, .drawer-only { display: none; }

@media (max-width: 1180px) {
    :root { --sidebar-w: 210px; --list-w: 300px; }
    .detail-inner { padding: 20px 24px 32px; }
    .footer > span { display: none; }
}

@media (max-width: 900px) {
    .sidebar {
        position: absolute; left: 0; top: 0; bottom: 0; z-index: 50; width: 270px;
        transform: translateX(-102%); transition: transform .2s; border-right: 0;
    }
    .app.show-sidebar .sidebar { transform: none; box-shadow: var(--shadow); }
    .app.show-sidebar .sidebar-backdrop { display: block; position: absolute; inset: 0; z-index: 45; background: rgba(15, 17, 20, .25); }
    .drawer-only { display: inline-grid; }
}

@media (max-width: 760px) {
    .app { padding: 0; }
    .rail { display: none; }
    .workspace { border-radius: 0; }
    .list-pane { width: 100%; border-right: 0; }
    .detail { display: none; }
    .app.show-detail .list-pane { display: none; }
    .app.show-detail .detail { display: flex; }
    .mobile-only { display: inline-grid; }
    .detail-inner { padding: 14px 16px 28px; }
    .detail-top, .form-head { margin: -4px -6px 8px; }
    .detail-hero { flex-direction: column; text-align: center; gap: 12px; }
    .detail-hero .chips { justify-content: center; }
    .row-actions { opacity: 1; }
    .fields-grid { grid-template-columns: 1fr; }
    .footer { display: none; }
    .lang-btn-mobile { display: inline-flex; align-items: center; height: 32px; padding: 0 8px; }
}
