:root {
    --primary: #2563eb;
    --primary-dark: #1e40af;
    --primary-light: #dbeafe;
    --teal: #0f766e;
    --success: #16a34a;
    --success-light: #dcfce7;
    --warn: #d97706;
    --warn-light: #fef3c7;
    --danger: #dc2626;
    --danger-light: #fee2e2;
    --ink: #1e293b;
    --muted: #64748b;
    --line: #e2e8f0;
    --card: #ffffff;
}

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

body {
    font-family: "Segoe UI", "Tahoma", "Noto Sans Thai", "Sarabun", Arial, sans-serif;
    color: var(--ink);
    background: linear-gradient(160deg, #eef2ff 0%, #f0fdfa 50%, #fefce8 100%);
    min-height: 100vh;
    background-attachment: fixed;
}

a { text-decoration: none; color: inherit; }

/* ---------- Navbar ---------- */
.navbar {
    background: linear-gradient(90deg, #1e3a8a, #0f766e);
    color: #fff;
    padding: 0 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 8px;
    box-shadow: 0 2px 12px rgba(15, 23, 42, .25);
    position: sticky;
    top: 0;
    z-index: 50;
}

.navbar .logo {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 1.15rem;
    font-weight: 700;
    letter-spacing: .3px;
    padding: 12px 0;
}

.navbar .logo .badge-clock {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: rgba(255, 255, 255, .16);
    border: 1px solid rgba(255, 255, 255, .35);
    display: grid;
    place-items: center;
    font-size: 1.05rem;
}

.nav-links { display: flex; gap: 6px; flex-wrap: wrap; }

.nav-links a {
    color: rgba(255, 255, 255, .85);
    padding: 8px 16px;
    border-radius: 999px;
    font-weight: 600;
    font-size: .92rem;
    transition: background .15s ease, color .15s ease;
}

.nav-links a:hover { background: rgba(255, 255, 255, .16); color: #fff; }

.nav-links a.active {
    background: #fff;
    color: var(--primary-dark);
}

/* ---------- Layout ---------- */
.container {
    max-width: 1080px;
    margin: 0 auto;
    padding: 24px 16px 56px;
}

.page-head { margin-bottom: 20px; }

.page-head h1 { font-size: 1.5rem; font-weight: 800; color: var(--primary-dark); }

.page-head p { color: var(--muted); margin-top: 4px; font-size: .95rem; }

.card {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 16px;
    box-shadow: 0 6px 24px rgba(15, 23, 42, .07);
    padding: 24px;
    margin-bottom: 20px;
}

.card-title {
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--primary-dark);
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.card-title .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--primary); display: inline-block; }

/* ---------- Clock ---------- */
.clock-card {
    text-align: center;
    padding: 34px 24px;
    background: linear-gradient(135deg, #312e81, #1e40af 45%, #0f766e);
    color: #fff;
    border: none;
}

.clock-time {
    font-size: 3.6rem;
    font-weight: 800;
    font-variant-numeric: tabular-nums;
    letter-spacing: 2px;
    line-height: 1.1;
    text-shadow: 0 3px 14px rgba(0, 0, 0, .35);
}

.clock-time small { font-size: 1.6rem; font-weight: 600; }

.clock-date {
    margin-top: 10px;
    font-size: 1.15rem;
    font-weight: 600;
    opacity: .95;
}

.clock-storage {
    margin-top: 8px;
    font-size: .8rem;
    opacity: .7;
}

/* ---------- Forms ---------- */
.field { margin-bottom: 16px; }

.field label {
    display: block;
    font-weight: 600;
    font-size: .92rem;
    color: var(--ink);
    margin-bottom: 6px;
}

.field label .req { color: var(--danger); }

select, input[type="text"], input[type="tel"] {
    width: 100%;
    padding: 11px 14px;
    font-size: 1rem;
    font-family: inherit;
    border: 1.5px solid var(--line);
    border-radius: 10px;
    background: #f8fafc;
    color: var(--ink);
    outline: none;
    transition: border-color .15s ease, box-shadow .15s ease;
}

select:focus, input:focus {
    border-color: var(--primary);
    background: #fff;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, .15);
}

.hint { font-size: .82rem; color: var(--muted); margin-top: 6px; }

.data-desc { color: var(--muted); line-height: 1.7; margin-bottom: 14px; font-size: .95rem; }

/* ---------- Buttons ---------- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    border: none;
    border-radius: 12px;
    padding: 12px 22px;
    font-size: 1rem;
    font-weight: 700;
    font-family: inherit;
    cursor: pointer;
    transition: transform .12s ease, box-shadow .18s ease, opacity .15s ease;
    color: #fff;
}

.btn:active { transform: translateY(1px); }

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

.btn-sm { padding: 8px 14px; font-size: .85rem; border-radius: 9px; }

.btn-primary { background: linear-gradient(135deg, #2563eb, #1d4ed8); box-shadow: 0 4px 12px rgba(37, 99, 235, .3); }
.btn-primary:hover:not(:disabled) { box-shadow: 0 6px 18px rgba(37, 99, 235, .4); }

.btn-success { background: linear-gradient(135deg, #16a34a, #15803d); box-shadow: 0 4px 12px rgba(22, 163, 74, .3); }
.btn-success:hover:not(:disabled) { box-shadow: 0 6px 18px rgba(22, 163, 74, .42); }

.btn-warn { background: linear-gradient(135deg, #f59e0b, #d97706); box-shadow: 0 4px 12px rgba(217, 119, 6, .3); }
.btn-warn:hover:not(:disabled) { box-shadow: 0 6px 18px rgba(217, 119, 6, .42); }

.btn-danger { background: linear-gradient(135deg, #ef4444, #dc2626); box-shadow: 0 4px 12px rgba(220, 38, 38, .3); }
.btn-danger:hover:not(:disabled) { box-shadow: 0 6px 18px rgba(220, 38, 38, .42); }

.btn-ghost {
    background: #e2e8f0;
    color: var(--ink);
    font-weight: 600;
}
.btn-ghost:hover:not(:disabled) { background: #cbd5e1; }

.btn-lg { padding: 14px 28px; font-size: 1.05rem; flex: 1; min-width: 160px; }

.btn-row { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 6px; }

.icon-del {
    background: #e2e8f0;
    color: var(--danger);
    border: none;
    border-radius: 8px;
    width: 30px;
    height: 30px;
    font-size: 1rem;
    font-weight: 800;
    cursor: pointer;
    font-family: inherit;
}
.icon-del:hover { background: var(--danger-light); }

/* ---------- Status panel ---------- */
.status-today {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 14px;
    margin-top: 4px;
}

.status-chip {
    border-radius: 12px;
    padding: 14px 16px;
    text-align: center;
}

.status-chip .chip-label { font-size: .8rem; font-weight: 700; opacity: .75; letter-spacing: .4px; }

.status-chip .chip-value { font-size: 1.45rem; font-weight: 800; margin-top: 4px; font-variant-numeric: tabular-nums; }

.status-chip .chip-device { font-size: .7rem; opacity: .7; margin-top: 4px; word-break: break-word; }

.chip-in { background: var(--success-light); color: #166534; }
.chip-out { background: var(--warn-light); color: #92400e; }

.emp-empty {
    border: 1.5px dashed var(--primary);
    background: var(--primary-light);
    color: var(--primary-dark);
    border-radius: 12px;
    padding: 16px;
    text-align: center;
    font-weight: 600;
    margin-top: 8px;
}

/* ---------- Modal ---------- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, .55);
    backdrop-filter: blur(3px);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding: 16px;
}

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

.modal-card {
    background: #fff;
    border-radius: 18px;
    box-shadow: 0 24px 60px rgba(15, 23, 42, .35);
    width: 100%;
    max-width: 440px;
    overflow: hidden;
    animation: pop .18s ease;
}

@keyframes pop {
    from { transform: scale(.94); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

.modal-head {
    padding: 18px 22px 14px;
    font-size: 1.1rem;
    font-weight: 800;
    display: flex;
    align-items: center;
    gap: 10px;
}

.modal-head.warn { color: #92400e; background: var(--warn-light); }
.modal-head.success { color: #166534; background: var(--success-light); }
.modal-head.info { color: #1e40af; background: var(--primary-light); }
.modal-head.danger { color: #7f1d1d; background: var(--danger-light); }

.modal-body { padding: 16px 22px; }

.modal-body p { margin-bottom: 10px; line-height: 1.65; }

.detail-line {
    display: flex;
    justify-content: space-between;
    padding: 8px 0;
    border-bottom: 1px dashed var(--line);
    font-size: .95rem;
}

.detail-line:last-child { border-bottom: none; }

.detail-line .k { color: var(--muted); font-weight: 600; }

.detail-line .v { font-weight: 700; text-align: right; }

.modal-actions {
    display: flex;
    gap: 10px;
    padding: 14px 22px 20px;
}

.modal-actions .btn { flex: 1; }

.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: #111827;
    color: #fff;
    padding: 12px 22px;
    border-radius: 12px;
    font-weight: 600;
    font-size: .95rem;
    box-shadow: 0 10px 30px rgba(0, 0, 0, .3);
    opacity: 0;
    pointer-events: none;
    transition: opacity .25s ease, transform .25s ease;
    z-index: 200;
    max-width: 90vw;
    text-align: center;
}

.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.toast.ok { background: #166534; }
.toast.err { background: #991b1b; }
.toast.warn { background: #92400e; }

/* ---------- Summary table ---------- */
.summary-controls {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 14px;
    align-items: end;
}

.table-wrap {
    overflow-x: auto;
    border: 1px solid var(--line);
    border-radius: 12px;
    margin-top: 6px;
}

table.summary {
    border-collapse: collapse;
    width: 100%;
    font-size: .82rem;
    white-space: nowrap;
}

table.summary th, table.summary td {
    border: 1px solid var(--line);
    padding: 7px 6px;
    text-align: center;
}

table.summary thead th {
    background: linear-gradient(90deg, #1e3a8a, #0f766e);
    color: #fff;
    font-weight: 700;
    font-size: .78rem;
    position: sticky;
    top: 0;
    z-index: 2;
}

table.summary thead th.dow {
    font-size: .68rem;
    font-weight: 600;
    padding: 3px 4px;
    background: rgba(255, 255, 255, .14);
    color: #cffafe;
    border-top: none;
}

table.summary thead th.name-col {
    position: sticky;
    left: 0;
    z-index: 3;
}

table.summary tbody th.name-col {
    position: sticky;
    left: 0;
    background: #f8fafc;
    z-index: 1;
    text-align: left;
    font-weight: 700;
    min-width: 170px;
    color: var(--ink);
}

table.summary tbody tr:nth-child(even) td { background: #fafbfc; }
table.summary tbody tr:nth-child(even) th.name-col { background: #f8fafc; }
table.summary tbody tr:hover td { background: var(--primary-light); }
table.summary tbody tr:hover th.name-col { background: var(--primary-light); }

.cell-in { color: #166534; font-weight: 700; display: block; }
.cell-out { color: #92400e; font-size: .74rem; display: block; }
.cell-only-out { color: #b45309; font-weight: 600; }
.cell-empty { color: #cbd5e1; }
.cell-dev {
    display: block;
    font-size: .6rem;
    font-weight: 600;
    color: #94a3b8;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 90px;
    margin: 1px auto 3px;
    line-height: 1.3;
}

td.col-count { font-weight: 800; color: var(--primary-dark); background: #eff6ff !important; }

/* ---------- Plain table ---------- */
table.plain {
    border-collapse: collapse;
    width: 100%;
    font-size: .92rem;
}

table.plain th, table.plain td { border-bottom: 1px solid var(--line); padding: 10px 12px; text-align: left; }

table.plain thead th {
    background: var(--primary-light);
    color: var(--primary-dark);
    font-size: .85rem;
    font-weight: 700;
}

/* ---------- Print ---------- */
@media print {
    body { background: #fff; }
    .navbar, .btn-row, .btn, .summary-controls, .hint, .card-title button, .card-title span#tableTitle { display: none !important; }
    .container { max-width: 100%; padding: 0; }
    .card { box-shadow: none; border: none; padding: 0; }
    #emptyState { display: none; }
    table.summary thead th, table.summary thead th.dow { position: static; }
    table.summary tbody th.name-col { position: static; background: #fff; }
    .table-wrap { overflow: visible; border: none; }
}

/* ---------- Misc ---------- */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; align-items: start; }

@media (max-width: 860px) {
    .grid-2, .summary-controls, .form-grid, .status-today { grid-template-columns: 1fr; }
    .clock-time { font-size: 2.6rem; }
}