/* ===== Tokens ===== */
:root {
    --bg: #f5f6f8;
    --surface: #ffffff;
    --surface-2: #f0f2f5;
    --border: #e1e4ea;
    --text: #1b1f27;
    --muted: #5d6573;
    --accent: #2457d6;
    --accent-hover: #1c47b3;
    --accent-soft: #e8eefc;
    --on-accent: #ffffff;
    --danger: #c42b2b;
    --danger-soft: #fdecec;
    --success: #1d7a46;
    --success-soft: #e7f5ec;
    --focus: 0 0 0 3px rgba(36, 87, 214, .28);
    --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
    --radius: 12px;
    --radius-sm: 8px;
    --topbar-h: 56px;
    --sidebar-w: 248px;
    --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --mono: ui-monospace, "Cascadia Mono", Consolas, "SF Mono", Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f1218;
        --surface: #171b23;
        --surface-2: #1f2430;
        --border: #2b3140;
        --text: #e7eaf0;
        --muted: #9aa3b2;
        --accent: #6c95ff;
        --accent-hover: #86a8ff;
        --accent-soft: #1d2741;
        --on-accent: #0b1020;
        --danger: #ff7b7b;
        --danger-soft: #3a1e22;
        --success: #5fd394;
        --success-soft: #16301f;
        --focus: 0 0 0 3px rgba(108, 149, 255, .35);
        --shadow: none;
    }
}

/* ===== Base ===== */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
    margin: 0;
    font-family: var(--font);
    font-size: 15px;
    line-height: 1.5;
    color: var(--text);
    background: var(--bg);
}
h1, h2 { margin: 0; line-height: 1.25; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.muted { color: var(--muted); }
:focus-visible { outline: none; box-shadow: var(--focus); border-radius: var(--radius-sm); }

/* ===== Top bar ===== */
.topbar {
    position: sticky; top: 0; z-index: 30;
    height: var(--topbar-h);
    display: flex; align-items: center; gap: 12px;
    padding: 0 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--text); font-weight: 650; font-size: 16px; }
.brand:hover { text-decoration: none; }
.brand-mark {
    display: grid; place-items: center;
    width: 30px; height: 30px; border-radius: 8px;
    background: var(--accent); color: var(--on-accent);
}
.topbar-end { margin-left: auto; display: flex; align-items: center; gap: 10px; min-width: 0; }
.topbar-end form { margin: 0; }
.brand span:last-child { white-space: nowrap; }
.user { color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 30vw; }
@media (max-width: 480px) { .user { display: none; } }

.icon-btn {
    display: grid; place-items: center;
    width: 40px; height: 40px; margin-left: -8px;
    border-radius: var(--radius-sm); color: var(--text); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }

/* ===== Sidebar (drawer trên mobile, cố định trên desktop) ===== */
.nav-toggle { position: absolute; opacity: 0; pointer-events: none; }
.sidebar {
    position: fixed; z-index: 40; top: 0; bottom: 0; left: 0;
    width: min(84vw, 300px);
    padding: 16px 12px calc(16px + env(safe-area-inset-bottom));
    overflow-y: auto;
    background: var(--surface);
    border-right: 1px solid var(--border);
    transform: translateX(-102%);
    transition: transform .2s ease;
}
.scrim {
    position: fixed; inset: 0; z-index: 35;
    background: rgba(10, 14, 22, .45);
    opacity: 0; pointer-events: none; transition: opacity .2s ease;
}
.nav-toggle:checked ~ .sidebar { transform: none; box-shadow: 0 10px 40px rgba(0,0,0,.25); }
.nav-toggle:checked ~ .scrim { opacity: 1; pointer-events: auto; }

.nav-group {
    margin: 18px 10px 6px;
    font-size: 12px; font-weight: 600; letter-spacing: .04em; text-transform: uppercase;
    color: var(--muted);
}
.nav-item {
    display: flex; align-items: center; gap: 10px;
    min-height: 42px; padding: 8px 10px;
    border-radius: var(--radius-sm);
    color: var(--text); font-weight: 500;
}
.nav-item svg { color: var(--muted); }
.nav-item:hover { background: var(--surface-2); text-decoration: none; }
.nav-item.active { background: var(--accent-soft); color: var(--accent); }
.nav-item.active svg { color: var(--accent); }

.content {
    width: 100%; max-width: 1080px;
    margin: 0 auto;
    padding: 20px 16px calc(32px + env(safe-area-inset-bottom));
}

@media (min-width: 900px) {
    .menu-btn, .scrim { display: none; }
    .sidebar {
        top: var(--topbar-h); z-index: 20;
        width: var(--sidebar-w);
        transform: none; transition: none; box-shadow: none !important;
    }
    .content { padding: 32px 40px 48px calc(var(--sidebar-w) + 40px); max-width: calc(1080px + var(--sidebar-w)); margin: 0; }
}

/* ===== Page ===== */
.page-head { margin-bottom: 20px; }
.page-head h1 { font-size: 24px; font-weight: 700; }
.page-head p { margin: 6px 0 0; }
.back-link { display: inline-block; margin-bottom: 8px; font-size: 14px; }
@media (min-width: 900px) { .back-link { display: none; } }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 20px;
}

/* Trang chủ */
.card-group + .card-group { margin-top: 28px; }
.group-title { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin-bottom: 10px; }
.card-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); }
.card-link { display: flex; flex-direction: column; gap: 6px; color: var(--text); transition: border-color .15s, transform .15s; }
.card-link:hover { text-decoration: none; border-color: var(--accent); }
.card-link:active { transform: scale(.99); }
.card-title { font-weight: 650; font-size: 16px; }
.card-desc { color: var(--muted); font-size: 14px; flex: 1; }
.card-cta { color: var(--accent); font-weight: 600; font-size: 14px; margin-top: 6px; }

/* ===== Form ===== */
.keygen-layout { display: grid; gap: 16px; align-items: start; }
@media (min-width: 1100px) { .keygen-layout { grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); gap: 20px; } }

.form-card { display: flex; flex-direction: column; gap: 16px; }
.field-row { display: grid; gap: 16px; }
.field-row.multi { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field label { font-weight: 600; font-size: 14px; }

.input {
    width: 100%;
    min-height: 46px;
    padding: 10px 12px;
    font: inherit; font-size: 16px; /* >= 16px: iOS không tự zoom */
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    transition: border-color .15s, box-shadow .15s;
}
.input::placeholder { color: var(--muted); opacity: .7; text-transform: none; letter-spacing: normal; }
.input:hover { border-color: color-mix(in srgb, var(--border), var(--text) 25%); }
.input:focus { outline: none; border-color: var(--accent); box-shadow: var(--focus); }
.input.mono { font-family: var(--mono); letter-spacing: .02em; }
.input.upper { text-transform: uppercase; }
.input.invalid { border-color: var(--danger); }
select.input {
    appearance: none;
    padding-right: 38px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%238892a0' stroke-width='2' stroke-linecap='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat; background-position: right 14px center;
}
.field-help { margin: 0; font-size: 13px; color: var(--muted); }
.field-error { margin: 0; font-size: 13px; color: var(--danger); }

.alert { margin: 0; padding: 10px 12px; border-radius: var(--radius-sm); font-size: 14px; }
.alert-error { background: var(--danger-soft); color: var(--danger); }

/* ===== Buttons ===== */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    min-height: 46px; padding: 10px 18px;
    font: inherit; font-weight: 600;
    border: 1px solid transparent; border-radius: var(--radius-sm);
    cursor: pointer; white-space: nowrap;
    transition: background .15s, border-color .15s, color .15s;
    -webkit-tap-highlight-color: transparent;
}
.btn:hover { text-decoration: none; }
.btn-primary { background: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary { background: var(--surface); border-color: var(--border); color: var(--text); }
.btn-secondary:hover { background: var(--surface-2); }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-sm { min-height: 36px; padding: 6px 12px; font-size: 14px; }
.btn-block { width: 100%; }
.copy-btn.copied { background: var(--success-soft); border-color: var(--success); color: var(--success); }

/* ===== Result ===== */
.result-title { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin-bottom: 12px; }
.result-card .empty { margin: 0; }
.result-card.has-key { border-color: color-mix(in srgb, var(--accent), transparent 55%); }
/* Key và nút Sao chép nằm cùng hàng nếu đủ chỗ, không thì nút xuống dưới; key không bị ngắt dòng giữa chừng. */
.key-box { display: flex; flex-wrap: wrap; gap: 10px; }
.key-value {
    flex: 999 1 auto; /* cùng hàng: key lấy gần hết chỗ trống; nút xuống dòng thì nút giãn hết chiều ngang */
    display: block;
    padding: 14px 16px;
    font-family: var(--mono); font-size: clamp(17px, 4.6vw, 22px); font-weight: 600; letter-spacing: .04em;
    text-align: center; white-space: nowrap; overflow-x: auto;
    background: var(--surface-2);
    border-radius: var(--radius-sm);
    user-select: all; -webkit-user-select: all;
}
.key-box .copy-btn { flex: 1 0 auto; }
.details { margin: 16px 0 0; display: grid; gap: 8px; }
.details div { display: flex; justify-content: space-between; gap: 16px; padding-top: 8px; border-top: 1px solid var(--border); font-size: 14px; }
.details dt { color: var(--muted); }
.details dd { margin: 0; font-weight: 600; text-align: right; font-family: var(--mono); }

/* ===== Auth ===== */
.auth-body { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.auth-main { width: 100%; max-width: 380px; }
.auth-brand { display: flex; align-items: center; justify-content: center; gap: 10px; margin-bottom: 20px; font-weight: 650; font-size: 18px; }
.auth-brand .brand-mark { width: 36px; height: 36px; }
.auth-card { display: flex; flex-direction: column; gap: 16px; padding: 24px; }
.auth-card h1 { font-size: 20px; }
.auth-card p { margin: 0; }

@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
