/* Infreeon Key Manager v2 */
:root {
    --bg: #f5f6fb;
    --bg-grad: radial-gradient(1200px 600px at 100% -10%, rgba(99, 102, 241, .10), transparent 60%),
               radial-gradient(900px 500px at -10% 110%, rgba(236, 72, 153, .06), transparent 60%);
    --surface: #ffffff;
    --surface-2: #f8f9fc;
    --surface-3: #eef0f6;
    --border: #e6e8f0;
    --border-strong: #d5d9e4;
    --text: #0f172a;
    --text-2: #475569;
    --muted: #8a94a8;
    --primary: #4f46e5;
    --primary-2: #7c3aed;
    --primary-soft: #eef0ff;
    --primary-ink: #3730a3;
    --success: #059669;
    --success-soft: #e7f8f1;
    --warning: #d97706;
    --warning-soft: #fff5e6;
    --danger: #dc2626;
    --danger-soft: #fdeeee;
    --whatsapp: #16a34a;
    --shadow-sm: 0 1px 2px rgba(15, 23, 42, .05);
    --shadow: 0 1px 2px rgba(15, 23, 42, .04), 0 6px 20px -8px rgba(15, 23, 42, .12);
    --shadow-lg: 0 24px 60px -20px rgba(15, 23, 42, .35);
    --radius: 14px;
    --radius-sm: 10px;
    --radius-xs: 7px;
    --sidebar: 256px;
    --font: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
    --mono: 'JetBrains Mono', ui-monospace, 'SFMono-Regular', Consolas, monospace;

    --t-year: #059669;   --t-year-soft: #e6f7f0;
    --t-month: #2563eb;  --t-month-soft: #e9f0ff;
    --t-trial: #d97706;  --t-trial-soft: #fff4e2;
    --t-passport: #0891b2; --t-passport-soft: #e3f6fa;
    --t-renewal: #c026d3; --t-renewal-soft: #fbeafd;
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #0a0c12;
        --bg-grad: radial-gradient(1200px 600px at 100% -10%, rgba(99, 102, 241, .16), transparent 60%),
                   radial-gradient(900px 500px at -10% 110%, rgba(236, 72, 153, .08), transparent 60%);
        --surface: #11141d; --surface-2: #151925; --surface-3: #1b2030;
        --border: #232939; --border-strong: #2f3649;
        --text: #e7eaf2; --text-2: #aab2c5; --muted: #6f7890;
        --primary: #7c83ff; --primary-2: #a78bfa; --primary-soft: #1d2040; --primary-ink: #c7caff;
        --success: #34d399; --success-soft: #0f2a22;
        --warning: #fbbf24; --warning-soft: #2c2210;
        --danger: #f87171; --danger-soft: #2d1415;
        --whatsapp: #22c55e;
        --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px -10px rgba(0, 0, 0, .6);
        --shadow-lg: 0 24px 60px -20px rgba(0, 0, 0, .8);
        --t-year: #34d399; --t-year-soft: #0f2a22;
        --t-month: #60a5fa; --t-month-soft: #111f38;
        --t-trial: #fbbf24; --t-trial-soft: #2c2210;
        --t-passport: #22d3ee; --t-passport-soft: #0d2830;
        --t-renewal: #e879f9; --t-renewal-soft: #2a1030;
        color-scheme: dark;
    }
}
:root[data-theme="dark"] {
    --bg: #0a0c12;
    --bg-grad: radial-gradient(1200px 600px at 100% -10%, rgba(99, 102, 241, .16), transparent 60%),
               radial-gradient(900px 500px at -10% 110%, rgba(236, 72, 153, .08), transparent 60%);
    --surface: #11141d; --surface-2: #151925; --surface-3: #1b2030;
    --border: #232939; --border-strong: #2f3649;
    --text: #e7eaf2; --text-2: #aab2c5; --muted: #6f7890;
    --primary: #7c83ff; --primary-2: #a78bfa; --primary-soft: #1d2040; --primary-ink: #c7caff;
    --success: #34d399; --success-soft: #0f2a22;
    --warning: #fbbf24; --warning-soft: #2c2210;
    --danger: #f87171; --danger-soft: #2d1415;
    --whatsapp: #22c55e;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px -10px rgba(0, 0, 0, .6);
    --shadow-lg: 0 24px 60px -20px rgba(0, 0, 0, .8);
    --t-year: #34d399; --t-year-soft: #0f2a22;
    --t-month: #60a5fa; --t-month-soft: #111f38;
    --t-trial: #fbbf24; --t-trial-soft: #2c2210;
    --t-passport: #22d3ee; --t-passport-soft: #0d2830;
    --t-renewal: #e879f9; --t-renewal-soft: #2a1030;
    color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; }
body {
    font-family: var(--font);
    font-size: 14px;
    line-height: 1.5;
    color: var(--text);
    background: var(--bg);
    background-image: var(--bg-grad);
    background-attachment: fixed;
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
    min-height: 100vh;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--primary); text-decoration: none; }
.mono { font-family: var(--mono); }
.muted { color: var(--muted); }
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* ---------- Layout ---------- */
.shell { display: grid; grid-template-columns: var(--sidebar) 1fr; min-height: 100vh; }
.sidebar {
    position: sticky; top: 0; height: 100vh;
    display: flex; flex-direction: column; gap: 6px;
    padding: 22px 16px;
    background: color-mix(in srgb, var(--surface) 82%, transparent);
    backdrop-filter: blur(14px);
    border-right: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 12px; padding: 4px 8px 22px; }
.brand-mark {
    width: 38px; height: 38px; border-radius: 11px; flex: none;
    display: grid; place-items: center; color: #fff; font-size: 16px;
    background: linear-gradient(135deg, var(--primary), var(--primary-2));
    box-shadow: 0 8px 20px -8px var(--primary);
}
.brand-name { font-weight: 800; font-size: 15px; letter-spacing: -.01em; }
.brand-sub { font-size: 11.5px; color: var(--muted); }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
    display: flex; align-items: center; gap: 12px;
    padding: 10px 12px; border-radius: var(--radius-sm);
    color: var(--text-2); font-weight: 600; cursor: pointer;
    border: 0; background: none; text-align: left; width: 100%;
    transition: background .15s, color .15s;
}
.nav-item i { width: 18px; text-align: center; font-size: 15px; }
.nav-item:hover { background: var(--surface-3); color: var(--text); }
.nav-item.active { background: var(--primary-soft); color: var(--primary-ink); }
.nav-item.active i { color: var(--primary); }
.nav-badge {
    margin-left: auto; font-size: 11px; font-weight: 700;
    background: var(--danger); color: #fff; border-radius: 999px; padding: 1px 7px;
}
.sidebar-foot { margin-top: auto; }
.user-chip {
    display: flex; align-items: center; gap: 10px; width: 100%;
    padding: 10px; border-radius: var(--radius-sm); border: 1px solid var(--border);
    background: var(--surface); cursor: pointer; text-align: left;
}
.user-chip:hover { border-color: var(--border-strong); }
.avatar {
    width: 34px; height: 34px; border-radius: 50%; flex: none;
    display: grid; place-items: center; font-weight: 700; color: #fff;
    background: linear-gradient(135deg, #f472b6, var(--primary-2));
}
.user-chip .name { font-weight: 700; font-size: 13px; }
.user-chip .role { font-size: 11.5px; color: var(--muted); text-transform: capitalize; }
.user-chip .fa-ellipsis-vertical { margin-left: auto; color: var(--muted); }

.main { min-width: 0; padding: 28px clamp(16px, 3vw, 40px) 60px; }
.page { max-width: 1240px; margin: 0 auto; animation: fadeUp .25s ease; }
@keyframes fadeUp { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 22px; }
.page-title { font-size: 26px; font-weight: 800; letter-spacing: -.02em; margin: 0; }
.page-sub { color: var(--muted); margin-top: 2px; }
.page-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.topbar, .bottom-nav { display: none; }

/* ---------- Cards / grid ---------- */
.card {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); box-shadow: var(--shadow);
}
.card-pad { padding: 20px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 18px 20px 0; }
.card-title { font-weight: 700; font-size: 15px; display: flex; align-items: center; gap: 8px; margin: 0; }
.card-title i { color: var(--muted); font-size: 13px; }
.card-body { padding: 16px 20px 20px; }
.grid { display: grid; gap: 16px; }
.grid-kpi { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-2 { grid-template-columns: 1.55fr 1fr; }
.grid-inv { grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); }
.stack { display: flex; flex-direction: column; gap: 16px; }

.kpi { padding: 18px 20px; position: relative; overflow: hidden; }
.kpi-label { color: var(--text-2); font-weight: 600; font-size: 12.5px; display: flex; align-items: center; gap: 8px; }
.kpi-icon { width: 28px; height: 28px; border-radius: 8px; display: grid; place-items: center; font-size: 12.5px; }
.kpi-value { font-size: 30px; font-weight: 800; letter-spacing: -.03em; margin-top: 10px; line-height: 1.1; }
.kpi-foot { font-size: 12px; color: var(--muted); margin-top: 6px; display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.delta { font-weight: 700; }
.delta.up { color: var(--success); }
.delta.down { color: var(--danger); }

/* ---------- Buttons ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    height: 38px; padding: 0 14px; border-radius: var(--radius-sm);
    border: 1px solid var(--border); background: var(--surface); color: var(--text);
    font-weight: 600; font-size: 13.5px; cursor: pointer; white-space: nowrap;
    transition: background .15s, border-color .15s, transform .05s, box-shadow .15s;
}
.btn:hover { border-color: var(--border-strong); background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary {
    background: linear-gradient(135deg, var(--primary), var(--primary-2)); border-color: transparent; color: #fff;
    box-shadow: 0 8px 18px -10px var(--primary);
}
.btn-primary:hover { background: linear-gradient(135deg, var(--primary), var(--primary-2)); filter: brightness(1.07); border-color: transparent; }
.btn-whatsapp { background: var(--whatsapp); border-color: transparent; color: #fff; }
.btn-whatsapp:hover { background: var(--whatsapp); filter: brightness(1.08); border-color: transparent; }
.btn-danger-ghost { color: var(--danger); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--surface-3); border-color: transparent; }
.btn-sm { height: 32px; padding: 0 10px; font-size: 12.5px; border-radius: 9px; }
.btn-lg { height: 48px; padding: 0 20px; font-size: 15px; border-radius: 12px; }
.btn-block { width: 100%; }
.icon-btn {
    width: 34px; height: 34px; padding: 0; border-radius: 9px;
    display: inline-grid; place-items: center; border: 1px solid transparent;
    background: transparent; color: var(--text-2); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-3); color: var(--text); }
.spinner {
    width: 16px; height: 16px; border-radius: 50%; flex: none;
    border: 2px solid currentColor; border-right-color: transparent;
    animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Forms ---------- */
.field { display: flex; flex-direction: column; gap: 6px; }
.label { font-size: 12.5px; font-weight: 600; color: var(--text-2); }
.input, .select, .textarea {
    width: 100%; height: 40px; padding: 0 12px;
    border-radius: var(--radius-sm); border: 1px solid var(--border-strong);
    background: var(--surface); color: var(--text); font-size: 14px;
    transition: border-color .15s, box-shadow .15s;
}
.textarea { height: auto; min-height: 96px; padding: 10px 12px; resize: vertical; line-height: 1.5; }
.select {
    appearance: none; padding-right: 32px; cursor: pointer;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238a94a8' stroke-width='2.5' stroke-linecap='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
    background-repeat: no-repeat; background-position: right 11px center;
}
.input:focus, .select:focus, .textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 18%, transparent); }
.input-icon { position: relative; }
.input-icon > i { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 13px; pointer-events: none; }
.input-icon .input { padding-left: 34px; }
.input-icon .input-clear { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); }
.hint { font-size: 12px; color: var(--muted); }
.error-text { font-size: 12.5px; color: var(--danger); font-weight: 500; }

.phone-input {
    display: flex; align-items: stretch; height: 56px;
    border: 1.5px solid var(--border-strong); border-radius: 14px; background: var(--surface);
    transition: border-color .15s, box-shadow .15s; overflow: hidden;
}
.phone-input:focus-within { border-color: var(--primary); box-shadow: 0 0 0 4px color-mix(in srgb, var(--primary) 15%, transparent); }
.phone-input.valid { border-color: var(--success); }
.phone-input.invalid { border-color: var(--danger); }
.phone-prefix { display: flex; align-items: center; gap: 6px; padding: 0 14px; background: var(--surface-2); border-right: 1px solid var(--border); font-weight: 700; color: var(--text-2); }
.phone-input input {
    flex: 1; min-width: 0; border: 0; outline: 0; background: transparent;
    font-size: 20px; font-weight: 600; letter-spacing: .06em; padding: 0 14px; font-family: var(--mono);
}
.phone-state { display: grid; place-items: center; padding: 0 14px; font-size: 18px; }

/* ---------- Badges / pills ---------- */
.badge {
    display: inline-flex; align-items: center; gap: 5px; height: 22px; padding: 0 8px;
    border-radius: 999px; font-size: 11.5px; font-weight: 700; white-space: nowrap;
}
.badge-success { background: var(--success-soft); color: var(--success); }
.badge-warning { background: var(--warning-soft); color: var(--warning); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-muted { background: var(--surface-3); color: var(--text-2); }
.badge-primary { background: var(--primary-soft); color: var(--primary); }
.dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; display: inline-block; }

.type-chip { display: inline-flex; align-items: center; gap: 6px; font-weight: 700; font-size: 12.5px; padding: 3px 9px 3px 7px; border-radius: 8px; white-space: nowrap; }
.type-ico { width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center; font-size: 13px; flex: none; }
[data-type="One Year"] { --tc: var(--t-year); --tcs: var(--t-year-soft); }
[data-type="One Month"] { --tc: var(--t-month); --tcs: var(--t-month-soft); }
[data-type="7-Days"] { --tc: var(--t-trial); --tcs: var(--t-trial-soft); }
[data-type="Passport"] { --tc: var(--t-passport); --tcs: var(--t-passport-soft); }
[data-type="Renewal Key"], [data-type="Renewal Key Send"] { --tc: var(--t-renewal); --tcs: var(--t-renewal-soft); }
.type-chip, .type-ico { color: var(--tc, var(--primary)); background: var(--tcs, var(--primary-soft)); }

.key-code {
    font-family: var(--mono); font-size: 12.5px; font-weight: 600; letter-spacing: .02em;
    padding: 3px 8px; border-radius: 7px; background: var(--surface-3); color: var(--text);
    display: inline-flex; align-items: center; gap: 6px; border: 0; cursor: pointer; max-width: 100%;
}
.key-code:hover { background: var(--primary-soft); color: var(--primary-ink); }
.key-code i { font-size: 11px; opacity: .6; }

/* ---------- Inventory ---------- */
.inv-card { padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.inv-top { display: flex; align-items: center; gap: 10px; }
.inv-name { font-weight: 700; }
.inv-sub { font-size: 12px; color: var(--muted); }
.inv-count { font-size: 28px; font-weight: 800; letter-spacing: -.03em; line-height: 1; }
.inv-count small { font-size: 12.5px; color: var(--muted); font-weight: 600; letter-spacing: 0; margin-left: 4px; }
.meter { height: 6px; border-radius: 99px; background: var(--surface-3); overflow: hidden; }
.meter > span { display: block; height: 100%; border-radius: inherit; background: var(--tc, var(--primary)); transition: width .5s ease; }
.inv-foot { display: flex; justify-content: space-between; align-items: center; font-size: 12px; color: var(--muted); gap: 8px; }

/* ---------- Chart ---------- */
.chart { width: 100%; height: 190px; display: block; }
.chart .bar { fill: var(--primary); transition: opacity .15s; }
.chart .bar-resent { fill: var(--primary-2); opacity: .35; }
.chart .bar-group:hover .bar { opacity: .8; }
.chart .axis { fill: var(--muted); font-size: 10.5px; font-family: var(--font); }
.chart .grid-line { stroke: var(--border); stroke-dasharray: 3 4; }
.chart .val { fill: var(--text-2); font-size: 10.5px; font-weight: 700; font-family: var(--font); }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }

/* ---------- Lists ---------- */
.list { display: flex; flex-direction: column; }
.list-row { display: flex; align-items: center; gap: 12px; padding: 12px 20px; border-top: 1px solid var(--border); }
.list-row:first-child { border-top: 0; }
.list-main { flex: 1; min-width: 0; }
.list-title { font-weight: 600; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.list-sub { font-size: 12.5px; color: var(--muted); }
.list-actions { display: flex; gap: 6px; flex: none; }
.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty i { font-size: 28px; margin-bottom: 10px; opacity: .6; display: block; }
.empty b { color: var(--text); display: block; margin-bottom: 4px; font-size: 15px; }

/* ---------- Send ---------- */
.send-layout { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr); gap: 20px; align-items: start; }
.send-layout > *, .grid > * { min-width: 0; }
.warn-box .btn, .info-box .btn { white-space: normal; height: auto; min-height: 32px; padding: 6px 10px; text-align: left; }
.type-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.type-option {
    position: relative; text-align: left; cursor: pointer;
    padding: 14px; border-radius: 14px; border: 1.5px solid var(--border);
    background: var(--surface); display: flex; flex-direction: column; gap: 10px;
    transition: border-color .15s, box-shadow .15s, transform .1s;
}
.type-option:hover { border-color: var(--border-strong); }
.type-option.selected { border-color: var(--tc); box-shadow: 0 0 0 4px color-mix(in srgb, var(--tc) 14%, transparent); }
.type-option.selected::after {
    content: '\f00c'; font-family: 'Font Awesome 6 Free'; font-weight: 900; font-size: 10px;
    position: absolute; top: 10px; right: 10px; width: 20px; height: 20px; border-radius: 50%;
    display: grid; place-items: center; background: var(--tc); color: #fff;
}
.type-option:disabled { opacity: .5; cursor: not-allowed; }
.type-option .t-name { font-weight: 700; }
.type-option .badge { align-self: flex-start; }
.type-option .t-meta { font-size: 12px; color: var(--muted); }
.step-label { display: flex; align-items: center; gap: 10px; font-weight: 700; margin: 0 0 12px; }
.step-num { width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; font-size: 11.5px; background: var(--primary-soft); color: var(--primary); }
.send-section + .send-section { margin-top: 24px; }
.warn-box, .info-box {
    display: flex; gap: 12px; padding: 12px 14px; border-radius: 12px; font-size: 13px; align-items: flex-start;
}
.warn-box { background: var(--warning-soft); color: var(--text); border: 1px solid color-mix(in srgb, var(--warning) 30%, transparent); }
.warn-box > i { color: var(--warning); margin-top: 2px; }
.info-box { background: var(--primary-soft); border: 1px solid color-mix(in srgb, var(--primary) 20%, transparent); }
.info-box > i { color: var(--primary); margin-top: 2px; }
.customer-key { display: flex; align-items: center; gap: 10px; padding: 12px 0; border-top: 1px dashed var(--border); }
.customer-key:first-child { border-top: 0; }

/* ---------- Validity ---------- */
.validity { min-width: 150px; }
.validity-top { display: flex; align-items: center; gap: 6px; font-weight: 700; font-size: 13px; }
.validity-sub { font-size: 11.5px; color: var(--muted); }
.validity .meter { margin: 5px 0 3px; height: 4px; }
.v-active { color: var(--success); --tc: var(--success); }
.v-expiring { color: var(--warning); --tc: var(--warning); }
.v-expired { color: var(--danger); --tc: var(--danger); }
.v-permanent { color: var(--t-passport); --tc: var(--t-passport); }

/* ---------- History ---------- */
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; padding: 14px; }
.toolbar .search { flex: 1 1 260px; }
.filter-panel { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; padding: 0 14px 14px; }
.presets { display: flex; gap: 6px; flex-wrap: wrap; grid-column: 1 / -1; }
.chip {
    display: inline-flex; align-items: center; gap: 6px; height: 28px; padding: 0 10px;
    border-radius: 999px; border: 1px solid var(--border); background: var(--surface);
    font-size: 12.5px; font-weight: 600; color: var(--text-2); cursor: pointer;
}
.chip:hover { border-color: var(--border-strong); color: var(--text); }
.chip.active { background: var(--primary-soft); border-color: color-mix(in srgb, var(--primary) 35%, transparent); color: var(--primary-ink); }
.chip .x { opacity: .6; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; padding: 0 14px 14px; }
.summary-bar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.stat-pill {
    display: inline-flex; align-items: center; gap: 8px; padding: 8px 12px; border-radius: 12px;
    background: var(--surface); border: 1px solid var(--border); cursor: pointer; font-weight: 600; font-size: 12.5px; color: var(--text-2);
}
.stat-pill b { font-size: 15px; color: var(--text); }
.stat-pill:hover { border-color: var(--border-strong); }
.stat-pill.active { border-color: var(--primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 12%, transparent); }

#hTable { container: htable / inline-size; }
.htable { width: 100%; }
.hrow {
    display: grid; align-items: center; gap: 14px;
    grid-template-columns: 124px minmax(200px, 1.4fr) 124px minmax(150px, 1fr) minmax(110px, 1fr) 128px;
    padding: 12px 16px; border-top: 1px solid var(--border);
}
.hrow > * { min-width: 0; }
.hrow.head { border-top: 0; background: var(--surface-2); font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); padding-top: 10px; padding-bottom: 10px; }
.hrow:not(.head):hover { background: var(--surface-2); }
.h-date b { display: block; font-weight: 600; font-size: 13px; }
.h-date span { font-size: 11.5px; color: var(--muted); }
.h-key { display: flex; flex-direction: column; gap: 5px; align-items: flex-start; min-width: 0; }
.h-key .tags { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.h-mobile { font-family: var(--mono); font-weight: 600; font-size: 13px; background: none; border: 0; padding: 0; cursor: pointer; color: var(--text); text-align: left; }
.h-mobile:hover { color: var(--primary); text-decoration: underline; }
.h-by { font-size: 11.5px; color: var(--muted); }
.h-remarks {
    background: none; border: 1px dashed transparent; border-radius: 8px; padding: 4px 6px; margin: -4px -6px;
    text-align: left; cursor: pointer; font-size: 12.5px; color: var(--text-2);
    display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; word-break: break-word;
}
.h-remarks:hover { border-color: var(--border-strong); }
.h-remarks.empty-remarks { color: var(--muted); }
.h-actions { display: flex; gap: 4px; justify-content: flex-end; }
/* The history table adapts to its own width (sidebar + tablet widths), not just the viewport. */
@container htable (max-width: 980px) {
    .hrow { grid-template-columns: 92px minmax(170px, 1.4fr) 106px minmax(128px, 1fr) 74px; gap: 12px; }
    .hrow .h-remarks-col { display: none; }
    .h-actions .btn-text { display: none; }
    .validity { min-width: 0; }
}
@container htable (max-width: 650px) {
    .hrow.head { display: none; }
    .hrow {
        grid-template-columns: 1fr auto; grid-template-areas: "key actions" "mobile validity" "date date" "remarks remarks";
        gap: 10px 12px; padding: 14px;
    }
    .hrow .h-key-col { grid-area: key; }
    .hrow .h-actions-col { grid-area: actions; align-self: start; }
    .hrow .h-mobile-col { grid-area: mobile; }
    .hrow .h-validity-col { grid-area: validity; justify-self: end; text-align: right; }
    .hrow .h-validity-col .validity-top { justify-content: flex-end; }
    .hrow .h-validity-col .validity { min-width: 130px; }
    .hrow .h-date-col { grid-area: date; }
    .hrow .h-date-col b, .hrow .h-date-col span { display: inline; font-size: 12px; font-weight: 500; color: var(--muted); }
    .hrow .h-date-col span::before { content: ' · '; }
    .hrow .h-remarks-col { grid-area: remarks; display: block; }
    .hrow .h-remarks-col .h-remarks.empty-remarks { display: none; }
}

.pager { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding: 12px 16px; border-top: 1px solid var(--border); }
.pager-info { font-size: 12.5px; color: var(--muted); display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.pager-info .select { height: 32px; width: auto; font-size: 12.5px; }
.pages { display: flex; gap: 4px; align-items: center; }
.page-btn { min-width: 32px; height: 32px; border-radius: 9px; border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-weight: 600; font-size: 12.5px; padding: 0 8px; }
.page-btn:hover:not(:disabled) { border-color: var(--border-strong); }
.page-btn.current { background: var(--primary); border-color: var(--primary); color: #fff; }
.page-btn:disabled { opacity: .45; cursor: not-allowed; }
.skeleton { background: linear-gradient(90deg, var(--surface-3), var(--surface-2), var(--surface-3)); background-size: 200% 100%; animation: shimmer 1.2s infinite; border-radius: 8px; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* ---------- Upload ---------- */
.dropzone {
    border: 2px dashed var(--border-strong); border-radius: 16px; padding: 28px 18px; text-align: center;
    background: var(--surface-2); cursor: pointer; transition: border-color .15s, background .15s;
}
.dropzone:hover, .dropzone.drag { border-color: var(--primary); background: var(--primary-soft); }
.dropzone i { font-size: 28px; color: var(--primary); margin-bottom: 8px; display: block; }
.dropzone b { display: block; font-size: 15px; }
.preview-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.preview-stats div { padding: 12px; border-radius: 12px; background: var(--surface-2); border: 1px solid var(--border); text-align: center; }
.preview-stats b { display: block; font-size: 22px; font-weight: 800; }
.preview-stats span { font-size: 12px; color: var(--muted); }

/* ---------- Modal / sheet ---------- */
.overlay {
    position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 20px;
    background: rgba(8, 10, 20, .5); backdrop-filter: blur(4px); animation: fade .15s ease;
}
@keyframes fade { from { opacity: 0; } }
.modal {
    width: min(520px, 100%); max-height: calc(100vh - 40px); overflow: auto;
    background: var(--surface); border: 1px solid var(--border); border-radius: 20px; box-shadow: var(--shadow-lg);
    animation: pop .2s cubic-bezier(.2, .9, .3, 1.2);
}
@keyframes pop { from { opacity: 0; transform: scale(.96) translateY(8px); } }
.modal-head { display: flex; align-items: flex-start; gap: 12px; padding: 20px 20px 0; }
.modal-head h3 { margin: 0; font-size: 18px; font-weight: 800; letter-spacing: -.01em; }
.modal-head p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.modal-head .icon-btn { margin-left: auto; }
.modal-body { padding: 18px 20px; display: flex; flex-direction: column; gap: 14px; }
.modal-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 0 20px 20px; flex-wrap: wrap; }
.sheet-grip { display: none; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-weight: 600; text-align: right; }

.success-hero { text-align: center; padding: 8px 0 4px; }
.success-icon {
    width: 64px; height: 64px; border-radius: 50%; margin: 0 auto 12px; display: grid; place-items: center;
    background: var(--success-soft); color: var(--success); font-size: 26px; animation: pop .35s cubic-bezier(.2, .9, .3, 1.4);
}
.big-key {
    font-family: var(--mono); font-size: clamp(16px, 4.5vw, 22px); font-weight: 600; letter-spacing: .04em;
    padding: 14px; border-radius: 14px; background: var(--surface-2); border: 1px dashed var(--border-strong);
    display: flex; align-items: center; justify-content: center; gap: 10px; word-break: break-all; cursor: pointer;
}
.msg-preview {
    white-space: pre-wrap; font-size: 12.5px; line-height: 1.55; max-height: 180px; overflow: auto;
    padding: 12px 14px; border-radius: 12px; background: #e7fbe9; color: #0b3b1d; border: 1px solid #c4ecc9;
}
:root[data-theme="dark"] .msg-preview { background: #0f2a18; color: #c9f2d3; border-color: #1c4b2b; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .msg-preview { background: #0f2a18; color: #c9f2d3; border-color: #1c4b2b; } }

/* ---------- Menu ---------- */
.menu {
    position: fixed; z-index: 120; min-width: 210px; padding: 6px;
    background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow-lg);
    animation: pop .15s ease;
}
.menu button {
    display: flex; align-items: center; gap: 10px; width: 100%; padding: 9px 10px; border: 0; background: none;
    border-radius: 9px; cursor: pointer; font-weight: 500; text-align: left;
}
.menu button:hover { background: var(--surface-3); }
.menu button i { width: 16px; text-align: center; color: var(--muted); }
.menu hr { border: 0; border-top: 1px solid var(--border); margin: 5px 0; }
.menu .danger, .menu .danger i { color: var(--danger); }

/* ---------- Toasts ---------- */
.toasts { position: fixed; z-index: 200; right: 20px; bottom: 20px; display: flex; flex-direction: column; gap: 8px; align-items: flex-end; pointer-events: none; }
.toast {
    pointer-events: auto; display: flex; align-items: center; gap: 10px; max-width: 420px;
    padding: 12px 14px; border-radius: 12px; background: #111827; color: #f9fafb; box-shadow: var(--shadow-lg);
    font-weight: 500; font-size: 13.5px; animation: toastIn .25s cubic-bezier(.2, .9, .3, 1.2);
}
.toast i { font-size: 15px; }
.toast.success i { color: #34d399; }
.toast.error i { color: #f87171; }
.toast.info i { color: #93c5fd; }
.toast.out { animation: toastOut .2s ease forwards; }
@keyframes toastIn { from { opacity: 0; transform: translateY(10px) scale(.97); } }
@keyframes toastOut { to { opacity: 0; transform: translateY(6px); } }

/* ---------- Login ---------- */
.login-wrap { min-height: 100vh; display: grid; grid-template-columns: 1.1fr 1fr; }
.login-art {
    position: relative; overflow: hidden; color: #fff; padding: 48px;
    display: flex; flex-direction: column; justify-content: space-between;
    background: radial-gradient(800px 500px at 20% 10%, #818cf8 0%, transparent 60%),
                radial-gradient(700px 500px at 90% 90%, #f472b6 0%, transparent 55%),
                linear-gradient(135deg, #312e81, #4c1d95);
}
.login-art h1 { font-size: clamp(30px, 3.4vw, 44px); line-height: 1.1; letter-spacing: -.03em; margin: 0 0 14px; font-weight: 800; }
.login-art p { opacity: .82; max-width: 420px; font-size: 15px; }
.login-feats { display: grid; gap: 12px; max-width: 420px; }
.login-feats div { display: flex; gap: 12px; align-items: center; padding: 12px 14px; border-radius: 14px; background: rgba(255, 255, 255, .1); border: 1px solid rgba(255, 255, 255, .15); backdrop-filter: blur(6px); }
.login-feats i { width: 32px; height: 32px; border-radius: 9px; display: grid; place-items: center; background: rgba(255, 255, 255, .16); }
.login-panel { display: grid; place-items: center; padding: 32px 20px; }
.login-card { width: min(400px, 100%); }
.login-card h2 { font-size: 26px; letter-spacing: -.02em; margin: 18px 0 4px; font-weight: 800; }
.login-card form { display: flex; flex-direction: column; gap: 14px; margin-top: 24px; }
.login-card .input { height: 46px; }
.pw-toggle { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
    .grid-kpi { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .grid-2, .send-layout { grid-template-columns: minmax(0, 1fr); }
    .filter-panel { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 860px) {
    .shell { display: block; }
    .sidebar { display: none; }
    .topbar {
        display: flex; align-items: center; gap: 10px; position: sticky; top: 0; z-index: 50;
        padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
        background: color-mix(in srgb, var(--surface) 85%, transparent); backdrop-filter: blur(14px);
        border-bottom: 1px solid var(--border);
    }
    .topbar .brand { padding: 0; }
    .topbar .brand-mark { width: 32px; height: 32px; font-size: 14px; border-radius: 9px; }
    .topbar .avatar { width: 32px; height: 32px; margin-left: auto; border: 0; cursor: pointer; }
    .bottom-nav {
        display: grid; grid-template-columns: repeat(4, 1fr); position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
        padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
        background: color-mix(in srgb, var(--surface) 90%, transparent); backdrop-filter: blur(14px);
        border-top: 1px solid var(--border);
    }
    .bottom-nav button {
        display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 6px 0; border: 0; background: none;
        font-size: 11px; font-weight: 600; color: var(--muted); cursor: pointer; position: relative;
    }
    .bottom-nav button i { font-size: 18px; }
    .bottom-nav button.active { color: var(--primary); }
    .bottom-nav .nav-badge { position: absolute; top: 0; left: 55%; margin: 0; font-size: 10px; padding: 0 5px; }
    .main { padding: 18px 16px calc(96px + env(safe-area-inset-bottom)); }
    .page-title { font-size: 22px; }
    .page-head { margin-bottom: 16px; }
    .login-wrap { grid-template-columns: 1fr; }
    .login-art { display: none; }

    .overlay { place-items: end center; padding: 0; }
    .modal {
        width: 100%; max-height: 92vh; border-radius: 22px 22px 0 0; border-bottom: 0;
        padding-bottom: env(safe-area-inset-bottom); animation: sheetUp .25s cubic-bezier(.2, .9, .3, 1);
    }
    .sheet-grip { display: block; width: 40px; height: 5px; border-radius: 99px; background: var(--border-strong); margin: 10px auto 0; }
    @keyframes sheetUp { from { transform: translateY(40px); opacity: .6; } }
    .modal-foot .btn { flex: 1; }
    .toasts { left: 16px; right: 16px; bottom: calc(84px + env(safe-area-inset-bottom)); align-items: stretch; }
    .toast { max-width: none; }
}

@media (max-width: 560px) {
    .grid-kpi { gap: 10px; }
    .kpi { padding: 14px; }
    .kpi-value { font-size: 24px; }
    .filter-panel { grid-template-columns: 1fr 1fr; }
    .filter-panel .field-wide { grid-column: 1 / -1; }
    .type-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .type-option { padding: 12px; }
    .card-pad { padding: 16px; }
    .phone-input input { font-size: 18px; letter-spacing: .03em; }
    .page-actions .btn .btn-text { display: none; }
    .list-row { padding: 12px 14px; }
    .card-head { padding: 16px 16px 0; }
    .card-body { padding: 14px 16px 16px; }
    .pager { justify-content: center; }
    .pages .page-num { display: none; }
    .pages .page-num.current { display: inline-block; }
    .summary-bar { flex-wrap: nowrap; overflow-x: auto; margin: 0 -16px 14px; padding: 2px 16px; scrollbar-width: none; }
    .summary-bar::-webkit-scrollbar { display: none; }
    .stat-pill { flex: none; }
    .key-code { font-size: 12px; letter-spacing: 0; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
