:root {
    color-scheme: light;
    --navy: #0f172a;
    --blue: #2563eb;
    --green: #15803d;
    --red: #b91c1c;
    --yellow: #ca8a04;
    --gray: #64748b;
    --surface: #ffffff;
    --background: #f1f5f9;
    --text: #0f172a;
    --muted: #64748b;
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html { min-height: 100%; background: var(--background); }

body {
    margin: 0;
    min-height: 100vh;
    min-height: 100dvh;
    color: var(--text);
    background: var(--background);
}

button, input { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }

.login-page {
    display: grid;
    place-items: center;
    padding: max(24px, env(safe-area-inset-top)) 20px max(24px, env(safe-area-inset-bottom));
    background:
        radial-gradient(circle at 15% 10%, rgba(37, 99, 235, .15), transparent 32rem),
        linear-gradient(145deg, #f8fafc, #e2e8f0);
}

.login-card {
    width: min(100%, 390px);
    padding: 34px 28px;
    border: 1px solid #e2e8f0;
    border-radius: 24px;
    background: rgba(255, 255, 255, .96);
    box-shadow: 0 24px 60px rgba(15, 23, 42, .12);
}

.brand-key {
    display: grid;
    width: 64px;
    height: 64px;
    margin: 0 auto 12px;
    place-items: center;
    border-radius: 20px;
    color: white;
    background: var(--navy);
    font-size: 44px;
    font-weight: 800;
    transform: rotate(-35deg);
}

.login-card h1 { margin: 0 0 26px; text-align: center; font-size: 2rem; }
.login-card form { display: grid; gap: 10px; }
.login-card label { font-weight: 700; font-size: .95rem; }
.login-card input {
    width: 100%;
    min-height: 52px;
    padding: 0 14px;
    margin-bottom: 8px;
    border: 1px solid #cbd5e1;
    border-radius: 12px;
    outline: none;
    background: white;
}
.login-card input:focus { border-color: var(--blue); box-shadow: 0 0 0 4px rgba(37, 99, 235, .12); }

.primary-button, .secondary-button, .danger-button {
    min-height: 50px;
    border: 0;
    border-radius: 12px;
    padding: 0 20px;
    cursor: pointer;
    font-weight: 800;
}
.primary-button { margin-top: 6px; color: white; background: var(--blue); }
.secondary-button { color: var(--text); background: #e2e8f0; }
.danger-button { color: white; background: var(--red); }
.primary-button:active, .secondary-button:active, .danger-button:active { transform: translateY(1px); }
.danger-button:disabled { opacity: .55; cursor: wait; }

.form-error, .success-message, .security-warning {
    padding: 12px 14px;
    border-radius: 10px;
    font-size: .92rem;
}
.form-error { color: #991b1b; background: #fee2e2; }
.success-message { color: #166534; background: #dcfce7; }
.security-warning { color: #854d0e; background: #fef9c3; }
.setup-card { width: min(100%, 480px); }

.panel-page { padding-bottom: env(safe-area-inset-bottom); }
.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 18px;
    padding: max(22px, env(safe-area-inset-top)) max(20px, calc((100% - 1160px) / 2)) 18px;
    color: white;
    background: var(--navy);
}
.eyebrow { margin: 0 0 3px; color: #94a3b8; font-size: .72rem; font-weight: 800; letter-spacing: .12em; text-transform: uppercase; }
.app-header h1 { margin: 0; font-size: clamp(1.55rem, 5vw, 2rem); }
.header-actions {
    display: flex;
    flex: 0 0 auto;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
}
.logout-form { margin: 0; }
.logout-link, .header-link {
    flex: 0 0 auto;
    padding: 10px 12px;
    border: 1px solid #475569;
    border-radius: 10px;
    color: #e2e8f0;
    background: transparent;
    text-decoration: none;
    font-size: .82rem;
    font-weight: 700;
    cursor: pointer;
}
.header-link { display: inline-flex; align-items: center; }

.panel-main { width: min(100%, 1200px); margin: 0 auto; padding: 20px; }
.connection-status {
    display: flex;
    width: fit-content;
    align-items: center;
    gap: 8px;
    margin: 0 0 18px auto;
    color: #166534;
    font-size: .86rem;
    font-weight: 700;
}
.connection-dot { width: 9px; height: 9px; border-radius: 50%; background: #22c55e; box-shadow: 0 0 0 4px #dcfce7; }
.connection-status.is-offline { color: #991b1b; }
.connection-status.is-offline .connection-dot { background: #ef4444; box-shadow: 0 0 0 4px #fee2e2; }

.operation-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.operation-card {
    position: relative;
    display: flex;
    min-width: 0;
    min-height: 170px;
    flex-direction: column;
    align-items: flex-start;
    justify-content: flex-end;
    overflow: hidden;
    padding: 20px;
    border: 0;
    border-radius: 20px;
    color: white;
    text-align: left;
    box-shadow: 0 10px 24px rgba(15, 23, 42, .14);
    cursor: pointer;
    transition: transform .15s ease, box-shadow .15s ease;
}
.operation-card:active { transform: scale(.98); }
.operation-card:focus-visible { outline: 4px solid #60a5fa; outline-offset: 3px; }
.operation-card strong { font-size: clamp(2rem, 9vw, 3.2rem); line-height: 1; letter-spacing: -.04em; }
.state-label { margin-top: 10px; font-size: 1rem; font-weight: 800; }
.last-seen { margin-top: 5px; overflow-wrap: anywhere; opacity: .88; font-size: .72rem; }
.status-indicator {
    position: absolute;
    top: 16px;
    right: 16px;
    width: 15px;
    height: 15px;
    border: 3px solid rgba(255, 255, 255, .8);
    border-radius: 50%;
    background: currentColor;
    box-shadow: 0 0 0 5px rgba(255, 255, 255, .15);
}
.state-active { background: linear-gradient(145deg, #16a34a, #166534); }
.state-off { background: linear-gradient(145deg, #dc2626, #991b1b); }
.state-processing { color: #1c1917; background: linear-gradient(145deg, #facc15, #eab308); }
.state-processing .status-indicator { border-color: rgba(28, 25, 23, .65); box-shadow: 0 0 0 5px rgba(28, 25, 23, .1); }
.state-offline { background: linear-gradient(145deg, #64748b, #475569); }

.modal-backdrop {
    position: fixed;
    z-index: 20;
    inset: 0;
    display: grid;
    place-items: center;
    padding: 20px;
    background: rgba(15, 23, 42, .66);
    backdrop-filter: blur(4px);
}
.modal-backdrop[hidden] { display: none; }
.modal {
    width: min(100%, 390px);
    padding: 26px;
    border-radius: 20px;
    background: white;
    box-shadow: 0 25px 70px rgba(0, 0, 0, .28);
}
.modal h2 { margin: 0 0 8px; }
.modal p { margin: 0 0 24px; color: var(--muted); }
.modal-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.toast {
    position: fixed;
    z-index: 30;
    right: 18px;
    bottom: max(18px, env(safe-area-inset-bottom));
    left: 18px;
    max-width: 460px;
    margin-left: auto;
    padding: 14px 16px;
    border-radius: 12px;
    color: white;
    background: var(--navy);
    box-shadow: 0 12px 35px rgba(15, 23, 42, .3);
    font-weight: 700;
}
.toast-error { background: #991b1b; }
.toast-success { background: #166534; }
.toast[hidden] { display: none; }

.keys-main {
    width: min(100%, 1040px);
    margin: 0 auto;
    padding: 22px 20px 40px;
}
.page-message {
    margin: 0 0 18px;
    padding: 13px 15px;
    border-radius: 12px;
    font-weight: 700;
}
.message-error { color: #991b1b; background: #fee2e2; }
.message-success { color: #166534; background: #dcfce7; }
.dark-eyebrow { color: var(--muted); }
.add-key-card {
    display: grid;
    gap: 22px;
    margin-bottom: 32px;
    padding: 22px;
    border: 1px solid #dbe3ee;
    border-radius: 20px;
    background: white;
    box-shadow: 0 10px 30px rgba(15, 23, 42, .07);
}
.add-key-card h2, .section-heading h2 { margin: 2px 0 8px; }
.add-key-card p { margin: 0; color: var(--muted); line-height: 1.5; }
.add-key-card form, .modal form { display: grid; gap: 9px; }
.add-key-card label, .modal label { font-size: .9rem; font-weight: 800; }
.add-key-card input, .modal input[type="password"] {
    width: 100%;
    min-height: 48px;
    padding: 0 13px;
    margin-bottom: 5px;
    border: 1px solid #cbd5e1;
    border-radius: 11px;
    outline: none;
}
.add-key-card input:focus, .modal input[type="password"]:focus {
    border-color: var(--blue);
    box-shadow: 0 0 0 4px rgba(37, 99, 235, .12);
}
.section-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 14px;
}
.key-count {
    display: grid;
    min-width: 38px;
    height: 38px;
    place-items: center;
    border-radius: 50%;
    color: #334155;
    background: #e2e8f0;
    font-weight: 900;
}
.keys-grid { display: grid; grid-template-columns: 1fr; gap: 14px; }
.key-card {
    padding: 20px;
    border: 1px solid #dbe3ee;
    border-left-width: 6px;
    border-radius: 17px;
    background: white;
    box-shadow: 0 8px 22px rgba(15, 23, 42, .06);
}
.key-active { border-left-color: var(--green); }
.key-inactive { border-left-color: var(--gray); }
.key-card-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}
.key-card h3 { margin: 0; font-size: 1.25rem; }
.key-badge {
    padding: 5px 9px;
    border-radius: 999px;
    color: #166534;
    background: #dcfce7;
    font-size: .76rem;
    font-weight: 900;
}
.key-inactive .key-badge { color: #475569; background: #e2e8f0; }
.current-key-label {
    width: fit-content;
    margin: 10px 0 0;
    padding: 4px 8px;
    border-radius: 7px;
    color: #1d4ed8;
    background: #dbeafe;
    font-size: .72rem;
    font-weight: 900;
}
.key-details { display: grid; gap: 10px; margin: 18px 0; }
.key-details div { display: flex; justify-content: space-between; gap: 14px; }
.key-details dt { color: var(--muted); font-size: .82rem; }
.key-details dd { margin: 0; text-align: right; font-size: .84rem; font-weight: 750; }
.key-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.key-actions button { min-width: 0; padding: 0 10px; }
.warning-button, .success-button {
    min-height: 50px;
    border: 0;
    border-radius: 12px;
    color: white;
    cursor: pointer;
    font-weight: 800;
}
.warning-button { background: #b45309; }
.success-button { background: var(--green); }
.warning-button:disabled {
    color: #64748b;
    background: #e2e8f0;
    cursor: not-allowed;
}

@media (min-width: 720px) {
    .operation-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 18px; }
    .operation-card { min-height: 210px; }
    .panel-main { padding: 28px 20px; }
    .add-key-card { grid-template-columns: .9fr 1.1fr; align-items: start; padding: 28px; }
    .keys-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }
    .keys-main { padding-top: 30px; }
}

@media (max-width: 520px) {
    .app-header { align-items: flex-start; }
    .header-actions { flex-direction: column; align-items: stretch; }
    .header-link, .logout-link { justify-content: center; padding: 8px 9px; font-size: .74rem; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}
