:root {
    --bg: #f5f5f5;
    --surface: #ffffff;
    --border: #e0e0e0;
    --text: #1a1a1a;
    --text-secondary: #666666;
    --text-muted: #999999;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --danger: #dc2626;
    --danger-hover: #b91c1c;
    --success: #16a34a;
    --warning: #d97706;
    --badge-red: #fecaca;
    --badge-red-text: #991b1b;
    --badge-green: #bbf7d0;
    --badge-green-text: #166534;
    --badge-yellow: #fef08a;
    --badge-yellow-text: #854d0e;
    --badge-blue: #bfdbfe;
    --badge-blue-text: #1e40af;
    --badge-grey: #e5e7eb;
    --badge-grey-text: #6b7280;
    --radius: 6px;
}

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

body {
    font-family:
        -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
        Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
}

/* ── Login ─────────────────────────────────────── */

#login-view {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
}

.login-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 2rem;
    width: 100%;
    max-width: 360px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.login-card h1 {
    font-size: 1.25rem;
    margin-bottom: 1.5rem;
    text-align: center;
}

.login-card .error {
    color: var(--danger);
    font-size: 0.85rem;
    margin-bottom: 0.75rem;
    display: none;
}

/* ── Forms ─────────────────────────────────────── */

.form-group {
    margin-bottom: 1rem;
}

.form-group label {
    display: block;
    font-size: 0.85rem;
    font-weight: 500;
    margin-bottom: 0.25rem;
    color: var(--text-secondary);
}

.form-group input,
.form-group select {
    width: 100%;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-family: inherit;
    background: var(--surface);
    color: var(--text);
}

.form-group input:focus,
.form-group select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.15);
}

.form-row {
    display: flex;
    gap: 0.75rem;
}

.form-row .form-group {
    flex: 1;
}

.uid-input {
    border: none;
    background: var(--bg);
    width: 100%;
    cursor: text;
    padding: 0.5rem 0.75rem;
    border-radius: var(--radius);
    font-size: 0.85rem;
}

.toggle-label {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
}

/* ── Buttons ───────────────────────────────────── */

.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.45rem 0.9rem;
    border: none;
    border-radius: var(--radius);
    font-size: 0.85rem;
    font-family: inherit;
    cursor: pointer;
    transition: background 0.15s;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
}

.btn-primary:hover:not(:disabled) {
    background: var(--primary-hover);
}

.btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.btn-danger {
    background: var(--danger);
    color: #fff;
}

.btn-danger:hover {
    background: var(--danger-hover);
}

.btn-sm {
    padding: 0.25rem 0.5rem;
    font-size: 0.8rem;
}

.btn-full {
    width: 100%;
    justify-content: center;
    padding: 0.6rem;
}

.btn-text-danger {
    background: none;
    border: none;
    color: var(--danger);
    font-size: 0.85rem;
    cursor: pointer;
    padding: 0.25rem 0;
    font-family: inherit;
}

.btn-text-danger:hover {
    text-decoration: underline;
}

/* ── Header & Nav ──────────────────────────────── */

#app-view {
    display: none;
}

.header {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 0 1.5rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 52px;
}

.header h1 {
    font-size: 1rem;
    font-weight: 600;
}

.header .user-info {
    display: flex;
    align-items: center;
    gap: 1rem;
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.tabs {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    padding: 0 1.5rem;
    gap: 0;
}

.tab {
    padding: 0.75rem 1.25rem;
    border: none;
    background: none;
    font-size: 0.85rem;
    font-family: inherit;
    cursor: pointer;
    color: var(--text-secondary);
    border-bottom: 2px solid transparent;
    transition:
        color 0.15s,
        border-color 0.15s;
}

.tab:hover {
    color: var(--text);
}

.tab.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
    font-weight: 500;
}

.tab .badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 1.25rem;
    height: 1.25rem;
    padding: 0 0.35rem;
    border-radius: 10px;
    font-size: 0.7rem;
    font-weight: 600;
    margin-left: 0.35rem;
    background: var(--badge-blue);
    color: var(--badge-blue-text);
}

/* ── Content ───────────────────────────────────── */

.content {
    padding: 1.5rem;
    max-width: 1200px;
    margin: 0 auto;
}

.section {
    display: none;
}

.section.active {
    display: block;
}

.toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 1rem;
    gap: 1rem;
}

.toolbar input[type="search"] {
    padding: 0.4rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.85rem;
    width: 250px;
    font-family: inherit;
}

.toolbar input[type="search"]:focus {
    outline: none;
    border-color: var(--primary);
}

/* ── Tables ────────────────────────────────────── */

.table-wrap {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
}

thead th {
    text-align: left;
    padding: 0.65rem 0.75rem;
    border-bottom: 1px solid var(--border);
    font-weight: 600;
    white-space: nowrap;
    color: var(--text-secondary);
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

tbody td {
    padding: 0.55rem 0.75rem;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}

tbody tr:last-child td {
    border-bottom: none;
}

tbody tr:nth-child(even) {
    background: #fafafa;
}

tbody tr:hover {
    background: #f0f4ff;
}

tbody tr.clickable-row {
    cursor: pointer;
}

td .actions {
    display: flex;
    gap: 0.35rem;
}

.empty-row td {
    text-align: center;
    padding: 2rem;
    color: var(--text-secondary);
}

/* ── Badges ────────────────────────────────────── */

.permission-badge {
    display: inline-block;
    padding: 0.15rem 0.5rem;
    border-radius: 10px;
    font-size: 0.75rem;
    font-weight: 500;
}

.permission-badge.prohibit {
    background: var(--badge-red);
    color: var(--badge-red-text);
}

.permission-badge.unlimited {
    background: var(--badge-green);
    color: var(--badge-green-text);
}

.permission-badge.time_limited {
    background: var(--badge-yellow);
    color: var(--badge-yellow-text);
}

/* ── Status dot (online indicator) ─────────────── */

.status-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-right: 0.35rem;
    vertical-align: middle;
}

.status-dot.online {
    background: var(--success);
    box-shadow: 0 0 0 2px rgba(22, 163, 74, 0.2);
    animation: pulse-dot 2s ease-in-out infinite;
}

.status-dot.offline {
    background: var(--badge-grey);
}

@keyframes pulse-dot {
    0%,
    100% {
        box-shadow: 0 0 0 2px rgba(22, 163, 74, 0.2);
    }
    50% {
        box-shadow: 0 0 0 4px rgba(22, 163, 74, 0.1);
    }
}

/* ── Sync badges ──────────────────────────────── */

.sync-badge {
    display: inline-block;
    padding: 0.15rem 0.5rem;
    border-radius: 10px;
    font-size: 0.75rem;
    font-weight: 500;
}

.sync-badge.synced {
    background: var(--badge-green);
    color: var(--badge-green-text);
}

.sync-badge.pending {
    background: var(--badge-yellow);
    color: var(--badge-yellow-text);
}

.sync-badge.syncing {
    background: var(--badge-blue);
    color: var(--badge-blue-text);
}

.sync-badge.failed {
    background: var(--badge-red);
    color: var(--badge-red-text);
}

/* ── Blocked badge ─────────────────────────────── */

.blocked-badge {
    display: inline-block;
    padding: 0.1rem 0.4rem;
    border-radius: 10px;
    font-size: 0.7rem;
    font-weight: 600;
    background: var(--badge-red);
    color: var(--badge-red-text);
    margin-left: 0.35rem;
}

/* ── Access summary badges ─────────────────────── */

.access-summary {
    display: flex;
    gap: 0.35rem;
    flex-wrap: wrap;
}

.access-summary .summary-badge {
    display: inline-block;
    padding: 0.1rem 0.4rem;
    border-radius: 10px;
    font-size: 0.72rem;
    font-weight: 500;
}

/* ── Key description column ────────────────────── */

.key-desc {
    font-weight: 600;
}

.key-uid-muted {
    font-family: "SF Mono", "Fira Code", "Cascadia Code", monospace;
    font-size: 0.78rem;
    color: var(--text-muted);
}

.blacklisted {
    color: var(--danger);
    font-weight: 500;
}

.day-check {
    color: var(--success);
    font-weight: bold;
}

.day-cross {
    color: var(--border);
}

.mono {
    font-family: "SF Mono", "Fira Code", "Cascadia Code", monospace;
    font-size: 0.8rem;
}

.text-muted {
    color: var(--text-muted);
    font-size: 0.8rem;
}

/* ── Toast notifications ──────────────────────── */

#toast-container {
    position: fixed;
    top: 60px;
    right: 1.5rem;
    z-index: 300;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    pointer-events: none;
}

.toast {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.75rem 1rem;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 0.85rem;
    pointer-events: auto;
    animation: toast-in 0.3s ease-out;
    max-width: 400px;
}

.toast.toast-out {
    animation: toast-out 0.3s ease-in forwards;
}

.toast .toast-text {
    flex: 1;
}

.toast .toast-text .mono {
    font-size: 0.75rem;
}

.toast .btn {
    flex-shrink: 0;
}

.toast .toast-close {
    background: none;
    border: none;
    font-size: 1rem;
    cursor: pointer;
    color: var(--text-muted);
    padding: 0 0.25rem;
    flex-shrink: 0;
}

@keyframes toast-in {
    from {
        opacity: 0;
        transform: translateX(20px);
    }
    to {
        opacity: 1;
        transform: translateX(0);
    }
}

@keyframes toast-out {
    from {
        opacity: 1;
        transform: translateX(0);
    }
    to {
        opacity: 0;
        transform: translateX(20px);
    }
}

/* ── Detail Panel (slide-in) ──────────────────── */

.detail-panel {
    position: fixed;
    top: 0;
    right: 0;
    width: 50%;
    max-width: 600px;
    height: 100vh;
    z-index: 150;
    transform: translateX(100%);
    transition: transform 0.25s ease-out;
    pointer-events: none;
}

.detail-panel.open {
    transform: translateX(0);
    pointer-events: auto;
}

.detail-panel-inner {
    display: flex;
    flex-direction: column;
    height: 100%;
    background: var(--surface);
    border-left: 1px solid var(--border);
    box-shadow: -4px 0 20px rgba(0, 0, 0, 0.08);
}

.detail-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.detail-panel-header h2 {
    font-size: 1rem;
}

.detail-panel-body {
    flex: 1;
    overflow-y: auto;
    padding: 1.25rem;
}

.detail-section {
    margin-bottom: 1.5rem;
}

.detail-section h3 {
    font-size: 0.9rem;
    font-weight: 600;
    margin-bottom: 0.75rem;
    color: var(--text);
}

.detail-section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.75rem;
}

.detail-section-header h3 {
    margin-bottom: 0;
}

.detail-section-actions {
    display: flex;
    gap: 0.35rem;
}

/* ── AR cards ──────────────────────────────────── */

.ar-card {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.65rem 0.75rem;
    margin-bottom: 0.5rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    cursor: pointer;
    transition: background 0.15s;
}

.ar-card:hover {
    background: #f0f4ff;
}

.ar-card-info {
    flex: 1;
    min-width: 0;
}

.ar-card-info .ar-lock-name {
    font-weight: 500;
    font-size: 0.85rem;
}

.ar-card-info .ar-details {
    font-size: 0.78rem;
    color: var(--text-secondary);
    display: flex;
    gap: 0.5rem;
    align-items: center;
    flex-wrap: wrap;
    margin-top: 0.15rem;
}

.ar-card.ar-pending {
    border-left: 3px solid var(--warning);
}

.ar-pending-badge {
    display: inline-block;
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--badge-yellow-text);
    background: var(--badge-yellow);
    padding: 0.15rem 0.5rem;
    border-radius: 10px;
    margin-left: 0.35rem;
}

/* ── Sync footer ──────────────────────────────── */

.detail-panel-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1.25rem;
    border-top: 1px solid var(--border);
    flex-shrink: 0;
    background: var(--bg);
}

.sync-info {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

/* ── Overlay for panel backdrop ───────────────── */

.panel-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.15);
    z-index: 140;
    opacity: 0;
    transition: opacity 0.25s;
    pointer-events: none;
}

.panel-backdrop.open {
    opacity: 1;
    pointer-events: auto;
}

/* ── Modal ─────────────────────────────────────── */

.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 200;
    align-items: center;
    justify-content: center;
}

.modal-overlay.open {
    display: flex;
}

.modal {
    background: var(--surface);
    border-radius: var(--radius);
    width: 100%;
    max-width: 500px;
    max-height: 85vh;
    overflow-y: auto;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border);
}

.modal-header h2 {
    font-size: 1rem;
}

.modal-close {
    background: none;
    border: none;
    font-size: 1.25rem;
    cursor: pointer;
    color: var(--text-secondary);
    padding: 0.25rem;
}

.modal-body {
    padding: 1.25rem;
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
    padding: 1rem 1.25rem;
    border-top: 1px solid var(--border);
}

/* ── Schedule group (AR modal) ────────────────── */

.schedule-group {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.75rem;
    margin-bottom: 1rem;
}

.schedule-heading {
    font-size: 0.8rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--text-secondary);
    margin-bottom: 0.75rem;
}

/* ── Copyright bar ─────────────────────────────── */

.copyright-bar {
    text-align: center;
    padding: 1.5rem 1rem;
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* ── Responsive ────────────────────────────────── */

@media (max-width: 768px) {
    .content {
        padding: 1rem;
    }

    .toolbar {
        flex-direction: column;
        align-items: stretch;
    }

    .toolbar input[type="search"] {
        width: 100%;
    }

    .tabs {
        overflow-x: auto;
        padding: 0 0.5rem;
    }

    .detail-panel {
        width: 100%;
        max-width: none;
    }
}
