:root {
    --bg: #f6f7fb;
    --surface: #ffffff;
    --surface-2: #f8fafc;
    --surface-3: #f1f5f9;
    --border: #e5e9f0;
    --border-strong: #d4dae5;
    --text: #0f172a;
    --text-2: #5b6b85;
    --text-3: #94a3b8;
    --primary: #4f46e5;
    --primary-hover: #4338ca;
    --primary-light: #eef2ff;
    --green: #15803d;
    --green-light: #ecfdf3;
    --red: #b91c1c;
    --red-light: #fef2f2;
    --yellow: #a16207;
    --yellow-light: #fefce8;
    --blue: #1d4ed8;
    --blue-light: #eff6ff;
    --purple: #6d28d9;
    --purple-light: #f5f3ff;
    --radius: 14px;
    --radius-sm: 9px;
    --radius-xs: 6px;
    --ring: 0 0 0 3px rgba(79, 70, 229, 0.16);
    --shadow: 0 1px 2px rgba(15, 23, 42, 0.05), 0 1px 3px rgba(15, 23, 42, 0.04);
    --shadow-md: 0 4px 12px -2px rgba(15, 23, 42, 0.08);
    --shadow-lg: 0 18px 40px -12px rgba(15, 23, 42, 0.25);
    --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    --mono: 'SF Mono', 'Fira Code', 'Consolas', ui-monospace, monospace;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html {
    color-scheme: light;
    -webkit-text-size-adjust: 100%;
}

body {
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    font-size: 14px;
    line-height: 1.55;
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.hidden { display: none !important; }

:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 99px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-3); }

/* ── Login ── */
.login-view {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    padding: 24px 20px;
    background:
        radial-gradient(900px 420px at 10% 0%, rgba(109, 40, 217, 0.45), transparent 60%),
        linear-gradient(140deg, #4338ca 0%, #4f46e5 45%, #6d28d9 100%);
}

.login-card {
    background: var(--surface);
    border-radius: 18px;
    box-shadow: var(--shadow-lg);
    padding: 36px 32px;
    width: 100%;
    max-width: 380px;
    text-align: center;
}

.login-icon {
    width: 56px;
    height: 56px;
    margin: 0 auto 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    border-radius: 16px;
    background: var(--primary-light);
}

.login-title {
    font-size: 20px;
    font-weight: 700;
    letter-spacing: -0.01em;
    margin-bottom: 4px;
}

.login-subtitle {
    font-size: 13px;
    color: var(--text-2);
    margin-bottom: 24px;
}

.login-form {
    display: flex;
    flex-direction: column;
    gap: 12px;
    text-align: left;
}

/* ── Forms ── */
.form-input, .form-input-sm, .form-select {
    width: 100%;
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    padding: 11px 14px;
    font-family: var(--font);
    font-size: 14px;
    color: var(--text);
    outline: none;
    transition: border-color 0.15s, box-shadow 0.15s;
}

.form-input::placeholder, .form-input-sm::placeholder { color: var(--text-3); }

.form-input:hover, .form-input-sm:hover, .form-select:hover { border-color: #c2cada; }

.form-input:focus, .form-input-sm:focus, .form-select:focus {
    border-color: var(--primary);
    box-shadow: var(--ring);
}

.form-input-sm { padding: 8px 12px; font-size: 13px; }

.form-select {
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='none' stroke='%235b6b85' stroke-width='2' stroke-linecap='round' viewBox='0 0 24 24'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
    padding-right: 34px;
    font-size: 13px;
}

.form-group { margin-bottom: 14px; }

.form-label {
    display: block;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-2);
    margin-bottom: 6px;
}

.form-hint {
    font-size: 11px;
    color: var(--text-3);
    margin-top: 5px;
}

.form-row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.form-error {
    color: var(--red);
    font-size: 13px;
    padding: 9px 12px;
    background: var(--red-light);
    border: 1px solid #fecaca;
    border-radius: var(--radius-sm);
}

/* ── Buttons ── */
.btn-primary, .btn-ghost, .btn-key {
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    font-family: var(--font);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, color 0.15s, box-shadow 0.15s;
    padding: 11px 18px;
    white-space: nowrap;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
    box-shadow: 0 1px 2px rgba(79, 70, 229, 0.3);
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:active { transform: translateY(1px); }
.btn-primary:disabled { opacity: 0.6; cursor: wait; }

.btn-block { width: 100%; }

.btn-ghost {
    background: var(--surface);
    color: var(--text-2);
    border-color: var(--border-strong);
}
.btn-ghost:hover { background: var(--surface-3); color: var(--text); }

.btn-sm { padding: 7px 13px; font-size: 12.5px; }

.btn-key {
    padding: 5px 11px;
    font-size: 11.5px;
    font-weight: 600;
    background: var(--surface);
    color: var(--text-2);
    border-color: var(--border-strong);
}
.btn-key:hover { background: var(--surface-3); color: var(--text); }
.btn-key.danger { color: var(--red); border-color: #fbcfcf; }
.btn-key.danger:hover { background: var(--red-light); }
.btn-key.warn { color: var(--yellow); border-color: #f5e0a3; }
.btn-key.warn:hover { background: var(--yellow-light); }

/* ── Layout ── */
.dashboard { min-height: 100vh; display: flex; flex-direction: column; }

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 12px 24px;
    background: rgba(255, 255, 255, 0.88);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 100;
}

.topbar-left { display: flex; align-items: center; gap: 11px; min-width: 0; }

.topbar-logo {
    width: 38px;
    height: 38px;
    flex: 0 0 38px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 19px;
    border-radius: 11px;
    background: var(--primary-light);
}

.topbar-title {
    font-size: 15px;
    font-weight: 700;
    letter-spacing: -0.01em;
    white-space: nowrap;
}
.topbar-sub { font-size: 11px; color: var(--text-3); }

.topbar-right { display: flex; align-items: center; gap: 14px; }

.topbar-status {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    font-size: 12px;
    font-weight: 500;
    color: var(--green);
    background: var(--green-light);
    border: 1px solid #c8ebd4;
    border-radius: 99px;
    padding: 4px 11px;
}

.dot {
    width: 7px;
    height: 7px;
    background: var(--green);
    border-radius: 50%;
    box-shadow: 0 0 0 3px rgba(21, 128, 61, 0.14);
}

.topbar-time {
    font-size: 12px;
    color: var(--text-2);
    font-family: var(--mono);
    font-variant-numeric: tabular-nums;
}

.main {
    width: 100%;
    max-width: 1180px;
    margin: 0 auto;
    padding: 24px;
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 18px;
}

/* ── Stats ── */
.stats {
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    gap: 14px;
}

.stat {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow);
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
    transition: border-color 0.15s, box-shadow 0.15s, transform 0.15s;
}
.stat:hover {
    border-color: var(--border-strong);
    box-shadow: var(--shadow-md);
    transform: translateY(-2px);
}

.stat-icon {
    width: 40px;
    height: 40px;
    flex: 0 0 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    border-radius: var(--radius-sm);
    background: var(--surface-3);
}
.stat-green .stat-icon { background: var(--green-light); }
.stat-red .stat-icon { background: var(--red-light); }
.stat-yellow .stat-icon { background: var(--yellow-light); }
.stat-blue .stat-icon { background: var(--blue-light); }
.stat-purple .stat-icon { background: var(--purple-light); }

.stat-info { min-width: 0; }

.stat-label {
    font-size: 11.5px;
    font-weight: 600;
    color: var(--text-2);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.stat-value {
    font-size: 24px;
    font-weight: 700;
    letter-spacing: -0.02em;
    font-variant-numeric: tabular-nums;
    line-height: 1.2;
}

/* ── Cards ── */
.grid {
    display: grid;
    grid-template-columns: 340px minmax(0, 1fr);
    gap: 18px;
    align-items: start;
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    padding: 14px 18px;
    border-bottom: 1px solid var(--border);
    background: var(--surface-2);
    flex-wrap: wrap;
}

.card-title {
    font-size: 14px;
    font-weight: 700;
    letter-spacing: -0.01em;
}

.card-subtitle {
    font-size: 11.5px;
    color: var(--text-3);
    margin-top: 1px;
}

.card-body { padding: 18px; }
.card-scroll { max-height: 520px; overflow-y: auto; }
.card-log { max-height: 320px; }

/* ── Generate ── */
.gen-form { display: block; }

.gen-result {
    margin-top: 18px;
    padding: 14px;
    border-radius: var(--radius-sm);
    background: var(--primary-light);
    border: 1px solid #dcd9fb;
    text-align: center;
}

.gen-result-label {
    font-size: 11px;
    font-weight: 700;
    color: var(--primary);
    margin-bottom: 8px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

.gen-key-box {
    font-family: var(--mono);
    font-size: 13px;
    font-weight: 600;
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-xs);
    padding: 10px;
    margin-bottom: 10px;
    word-break: break-all;
    user-select: all;
    cursor: pointer;
}
.gen-key-box:hover { border-color: var(--primary); color: var(--primary); }

/* ── Filter bar ── */
.filter-bar { display: flex; gap: 8px; align-items: center; }
.filter-bar .form-input-sm { width: 150px; }
.filter-bar .form-select { width: auto; }

/* ── Keys list ── */
.keys-list { display: flex; flex-direction: column; gap: 10px; }

.key-item {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 14px;
    transition: border-color 0.15s, background 0.15s;
}
.key-item:hover { border-color: var(--border-strong); background: var(--surface); }

.key-item-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 10px;
    margin-bottom: 10px;
}

.key-value-display {
    font-family: var(--mono);
    font-size: 13px;
    font-weight: 600;
    word-break: break-all;
    cursor: pointer;
    border-bottom: 1px dashed var(--border-strong);
    transition: color 0.15s, border-color 0.15s;
}
.key-value-display:hover { color: var(--primary); border-color: var(--primary); }

.badge {
    font-size: 10px;
    font-weight: 700;
    padding: 4px 9px;
    border-radius: 99px;
    white-space: nowrap;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}
.badge.active { background: var(--green-light); color: var(--green); }
.badge.revoked { background: var(--red-light); color: var(--red); }
.badge.expired { background: var(--yellow-light); color: var(--yellow); }

.key-meta {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
    gap: 4px 16px;
    font-size: 12px;
    color: var(--text-2);
    margin-bottom: 10px;
}
.key-meta b { color: var(--text-3); font-weight: 600; }
.key-meta .mono { font-family: var(--mono); font-size: 11.5px; word-break: break-all; }

.key-actions { display: flex; gap: 6px; flex-wrap: wrap; }

.key-note {
    font-size: 12.5px;
    color: var(--text-2);
    margin-bottom: 10px;
    padding: 7px 10px;
    background: var(--surface);
    border-left: 2px solid var(--border-strong);
    border-radius: var(--radius-xs);
}

/* ── Logs ── */
.logs-list { font-size: 12.5px; }

.log-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 9px 4px;
    border-bottom: 1px solid var(--border);
}
.log-item:last-child { border-bottom: none; }

.log-time {
    color: var(--text-3);
    font-family: var(--mono);
    font-size: 11.5px;
    white-space: nowrap;
    flex: 0 0 auto;
}

.log-result {
    font-weight: 700;
    font-size: 10px;
    padding: 3px 8px;
    border-radius: 99px;
    white-space: nowrap;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    flex: 0 0 auto;
}
.log-result.success { background: var(--green-light); color: var(--green); }
.log-result.invalid_key,
.log-result.expired,
.log-result.max_uses_reached,
.log-result.device_mismatch { background: var(--red-light); color: var(--red); }
.log-result.missing_key { background: var(--yellow-light); color: var(--yellow); }

.log-key {
    font-family: var(--mono);
    font-size: 11.5px;
    color: var(--text-2);
    word-break: break-all;
    flex: 1 1 auto;
    min-width: 0;
}

.log-ip {
    color: var(--text-3);
    font-family: var(--mono);
    font-size: 11.5px;
    white-space: nowrap;
    flex: 0 0 auto;
}

/* ── Empty ── */
.empty {
    text-align: center;
    color: var(--text-3);
    padding: 44px 20px;
    font-size: 13px;
}

/* ── Footer ── */
.footer {
    text-align: center;
    padding: 18px;
    font-size: 12px;
    color: var(--text-3);
    border-top: 1px solid var(--border);
    background: var(--surface);
}

/* ── Toast ── */
#toast-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-width: calc(100vw - 40px);
}

.toast {
    background: var(--text);
    color: #fff;
    padding: 11px 18px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-weight: 500;
    box-shadow: var(--shadow-md);
    animation: toast-in 0.2s ease;
}
.toast.error { background: var(--red); }
.toast.success { background: var(--green); }
.toast.removing { animation: toast-out 0.2s ease forwards; }

@keyframes toast-in {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}
@keyframes toast-out {
    to { opacity: 0; transform: translateY(10px); }
}

/* ── Responsive ── */
@media (max-width: 1100px) {
    .stats { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    .grid { grid-template-columns: 300px minmax(0, 1fr); }
}

@media (max-width: 900px) {
    .grid { grid-template-columns: 1fr; }
    .card-scroll { max-height: 420px; }
}

@media (max-width: 720px) {
    .topbar {
        flex-wrap: wrap;
        padding: 10px 16px;
        gap: 10px;
    }
    .topbar-right {
        width: 100%;
        justify-content: space-between;
        gap: 10px;
        padding-top: 10px;
        border-top: 1px solid var(--border);
    }
    .main { padding: 16px; gap: 14px; }
    .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
    .stat { padding: 12px; }
    .stat-value { font-size: 20px; }
    .card-body { padding: 14px; }
    .filter-bar { width: 100%; flex-wrap: wrap; }
    .filter-bar .form-input-sm { flex: 1 1 100%; width: 100%; }
    .filter-bar .form-select { flex: 1 1 auto; }
    .form-row-2 { grid-template-columns: 1fr; }
    .log-item { flex-wrap: wrap; gap: 6px 10px; }
    .log-key { flex: 1 1 100%; order: 3; }
    #toast-container { left: 16px; right: 16px; bottom: 16px; }
}

@media (max-width: 400px) {
    .login-card { padding: 28px 20px; }
    .stats { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    .stat:hover { transform: none; }
}
