/* =========================================================================
   License Manager — stylesheet
   โทนสีน้ำเงิน-ม่วง แบบ modern พร้อม dark mode ตาม prefers-color-scheme
   ========================================================================= */

:root {
    --bg:        #f4f6fb;
    --surface:   #ffffff;
    --surface-2: #f8fafc;
    --border:    #e2e8f0;
    --text:      #1e293b;
    --text-soft: #64748b;
    --primary:   #4f46e5;
    --primary-d: #4338ca;
    --danger:    #dc2626;
    --danger-bg: #fef2f2;
    --success:   #16a34a;
    --success-bg:#f0fdf4;
    --warning:   #d97706;
    --warning-bg:#fffbeb;
    --shadow:    0 1px 3px rgba(15, 23, 42, .08), 0 1px 2px rgba(15, 23, 42, .06);
    --radius:    12px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg:        #0f172a;
        --surface:   #1e293b;
        --surface-2: #263449;
        --border:    #334155;
        --text:      #e2e8f0;
        --text-soft: #94a3b8;
        --primary:   #6366f1;
        --primary-d: #818cf8;
        --danger:    #f87171;
        --danger-bg: #3b1d1d;
        --success:   #4ade80;
        --success-bg:#14311f;
        --warning:   #fbbf24;
        --warning-bg:#3a2c12;
        --shadow:    0 1px 3px rgba(0,0,0,.4);
    }
}

* { box-sizing: border-box; }

html {
    min-width: 0;
    -webkit-text-size-adjust: 100%;
}

body {
    margin: 0;
    min-width: 0;
    min-height: 100vh;
    min-height: 100svh;
    overflow-x: hidden;
    font-family: 'Segoe UI', Tahoma, 'Sarabun', sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
}

.container { width: 100%; max-width: 960px; margin: 0 auto; padding: 0 20px; }

/* =========================================================================
   App layout — เมนูด้านซ้าย (sidebar)
   ========================================================================= */
.layout { display: flex; min-height: 100vh; min-height: 100svh; }
.layout--guest { display: block; }

.sidebar {
    width: 252px; flex-shrink: 0;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex; flex-direction: column;
    position: sticky; top: 0; height: 100vh; z-index: 40;
}
.sidebar__brand {
    display: flex; align-items: center; gap: 10px;
    padding: 18px 20px; text-decoration: none; color: var(--text);
    font-weight: 800; font-size: 1.12rem; letter-spacing: -.01em;
    border-bottom: 1px solid var(--border);
}
.sidebar__logo { font-size: 1.4rem; line-height: 1; }

.sidebar__nav { flex: 1; overflow-y: auto; padding: 14px 12px; display: flex; flex-direction: column; gap: 3px; }
.sidebar__label {
    font-size: .68rem; font-weight: 700; text-transform: uppercase; letter-spacing: .09em;
    color: var(--text-soft); padding: 16px 12px 6px;
}
.side-link {
    display: flex; align-items: center; gap: 11px;
    padding: 10px 12px; border-radius: 9px;
    color: var(--text-soft); text-decoration: none; font-weight: 600; font-size: .93rem;
    transition: background .15s, color .15s;
}
.side-link__ic { font-size: 1.05rem; width: 22px; text-align: center; flex-shrink: 0; }
.side-link:hover { background: var(--surface-2); color: var(--text); }
.side-link.is-active { background: color-mix(in srgb, var(--primary) 14%, transparent); color: var(--primary); }
.side-link--admin.is-active { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }

.sidebar__foot { border-top: 1px solid var(--border); padding: 12px; }
.usercard { display: flex; align-items: center; gap: 10px; }
.usercard__avatar {
    width: 38px; height: 38px; border-radius: 50%; flex-shrink: 0;
    display: grid; place-items: center; font-weight: 700; color: #fff; font-size: 1rem;
    background: linear-gradient(135deg, var(--primary), var(--primary-d));
}
.usercard__meta { flex: 1; min-width: 0; }
.usercard__name { font-weight: 700; font-size: .9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.usercard__role { font-size: .78rem; color: var(--text-soft); }
.usercard__logout { margin: 0; }
.icon-btn {
    background: transparent; border: 1px solid var(--border); color: var(--text-soft);
    width: 34px; height: 34px; border-radius: 8px; cursor: pointer; font-size: 1rem;
    display: grid; place-items: center; transition: background .15s, border-color .15s, color .15s;
}
.icon-btn:hover { border-color: var(--danger); color: var(--danger); background: var(--danger-bg); }

.content { flex: 1; min-width: 0; min-height: 100vh; min-height: 100svh; display: flex; flex-direction: column; }
.main { flex: 1; width: 100%; max-width: 1100px; margin: 0 auto; padding: 30px 34px 60px; }
.main--center { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; }
.auth-brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 1.4rem; color: var(--text); text-decoration: none; }
.main--center > .flash { width: min(100%, 420px); }

.appfooter {
    border-top: 1px solid var(--border); color: var(--text-soft);
    text-align: center; padding: 16px; font-size: .82rem;
    display: flex; gap: 8px; justify-content: center; flex-wrap: wrap;
}

/* mobile bar + off-canvas sidebar (hidden on desktop) */
.mobilebar { display: none; }
.scrim { display: none; }

@media (max-width: 860px) {
    .sidebar {
        position: fixed; top: 0; left: 0; height: 100vh;
        height: 100dvh; width: min(86vw, 300px);
        transform: translateX(-100%); transition: transform .25s ease;
        box-shadow: 0 12px 40px rgba(0,0,0,.28);
    }
    .nav-toggle:checked ~ .sidebar { transform: translateX(0); }
    .nav-toggle:checked ~ .content .scrim {
        display: block; position: fixed; inset: 0; z-index: 30; background: rgba(15,23,42,.5);
    }
    .mobilebar {
        display: flex; align-items: center; gap: 12px; height: 56px; padding: 0 14px;
        padding-left: max(14px, env(safe-area-inset-left));
        padding-right: max(14px, env(safe-area-inset-right));
        background: var(--surface); border-bottom: 1px solid var(--border);
        position: sticky; top: 0; z-index: 20;
    }
    .mobilebar__title { font-weight: 700; display: flex; align-items: center; gap: 8px; }
    .hamburger {
        display: inline-flex; flex-direction: column; justify-content: center; gap: 4px;
        width: 40px; height: 40px; cursor: pointer; margin: 0 -8px 0 0;
    }
    .hamburger span { display: block; height: 2px; width: 22px; background: var(--text); border-radius: 2px; }
    .main {
        padding: 20px max(16px, env(safe-area-inset-right)) calc(48px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
    }
}

/* Device rows become readable cards before the desktop table gets cramped. */
@media (max-width: 760px) {
    .device-table {
        display: block;
        min-width: 0;
        border: 0;
    }

    .device-table thead { display: none; }
    .device-table tbody { display: grid; gap: 12px; }
    .device-table tr {
        display: grid;
        gap: 0;
        padding: 12px 14px;
        background: var(--surface-2);
        border: 1px solid var(--border);
        border-radius: 10px;
    }

    .device-table td {
        display: grid;
        grid-template-columns: minmax(104px, 28%) minmax(0, 1fr);
        gap: 12px;
        align-items: start;
        min-width: 0;
        padding: 9px 0;
        border-bottom: 1px solid var(--border);
        overflow-wrap: anywhere;
    }

    .device-table td:last-child { border-bottom: 0; }
    .device-table td::before {
        content: attr(data-label);
        color: var(--text-soft);
        font-size: .78rem;
        font-weight: 700;
        line-height: 1.4;
    }

    .device-table .mono {
        display: block;
        max-width: 100%;
        margin-top: 3px;
        overflow-wrap: anywhere;
        word-break: break-word !important;
    }

    .device-table .row-actions { flex-wrap: wrap; gap: 8px; }
    .device-table .row-actions form { flex: 1 1 120px; }
    .device-table .row-actions .btn { width: 100%; }
}

/* ---------- Page head ---------- */
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 24px; flex-wrap: wrap; }
.page-head h1 { margin: 0; font-size: 1.6rem; }
.page-head p { margin: 4px 0 0; color: var(--text-soft); }

/* ---------- Cards ---------- */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 24px;
}
.card + .card { margin-top: 20px; }

/* ---------- Auth card ---------- */
.auth-wrap { width: 100%; max-width: 420px; margin: 40px auto; }
.auth-wrap h1 { margin-top: 0; text-align: center; }
.auth-wrap .muted { text-align: center; }

/* ---------- Stats ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 16px; margin-bottom: 28px; }
.stat {
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 18px 20px; box-shadow: var(--shadow);
}
.stat__num { font-size: 1.9rem; font-weight: 700; line-height: 1; }
.stat__label { color: var(--text-soft); font-size: .85rem; margin-top: 6px; }
.stat--warn .stat__num { color: var(--warning); }
.stat--danger .stat__num { color: var(--danger); }

/* ---------- Forms ---------- */
.form-group { margin-bottom: 16px; }
.form-row { display: flex; gap: 16px; flex-wrap: wrap; }
.form-row .form-group { flex: 1; min-width: 180px; }
label { display: block; font-weight: 600; margin-bottom: 6px; font-size: .9rem; }
label .req { color: var(--danger); }
input, select, textarea {
    min-width: 0;
    width: 100%; padding: 10px 12px; font: inherit; color: var(--text);
    background: var(--surface-2); border: 1px solid var(--border);
    border-radius: 8px; transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus, textarea:focus {
    outline: none; border-color: var(--primary);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 20%, transparent);
}
textarea { resize: vertical; min-height: 80px; }
.hint { color: var(--text-soft); font-size: .8rem; margin-top: 4px; }
.input-with-btn { display: flex; gap: 8px; }
.input-with-btn input { flex: 1; }

/* ---------- Buttons ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    padding: 10px 18px; font: inherit; font-weight: 600; cursor: pointer;
    border: 1px solid transparent; border-radius: 8px; text-decoration: none;
    transition: background .15s, opacity .15s, border-color .15s;
}
.btn--sm { padding: 6px 12px; font-size: .85rem; }
.btn--primary { background: var(--primary); color: #fff; }
.btn--primary:hover { background: var(--primary-d); }
.btn--ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn--ghost:hover { background: var(--surface-2); }
.btn--danger { background: var(--danger); color: #fff; }
.btn--danger:hover { opacity: .9; }
.btn--block { width: 100%; }
.btn-row { display: flex; gap: 10px; margin-top: 8px; }

/* ---------- Flash messages ---------- */
.flash { padding: 12px 16px; border-radius: 8px; margin-bottom: 18px; font-weight: 500; border: 1px solid transparent; }
.flash--success { background: var(--success-bg); color: var(--success); border-color: var(--success); }
.flash--error   { background: var(--danger-bg);  color: var(--danger);  border-color: var(--danger); }
.flash--info    { background: var(--surface-2);  color: var(--text-soft); border-color: var(--border); }

/* ---------- License list ---------- */
.toolbar { display: flex; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
.toolbar form { display: flex; gap: 8px; flex: 1; min-width: 220px; }
.toolbar input { flex: 1; }

.license-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }
.license {
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    box-shadow: var(--shadow); padding: 18px; display: flex; flex-direction: column; gap: 10px;
}
.license__head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.license__name { font-weight: 700; font-size: 1.05rem; margin: 0; }
.license__ver { color: var(--text-soft); font-size: .82rem; }
.license__key {
    font-family: 'Consolas', ui-monospace, monospace; background: var(--surface-2);
    border: 1px dashed var(--border); border-radius: 8px; padding: 8px 10px;
    font-size: .92rem; letter-spacing: .5px; word-break: break-all;
    display: flex; align-items: flex-start; gap: 10px;
}
.license__key-value { flex: 1; min-width: 0; }
.copy-btn {
    flex: 0 0 auto;
    display: inline-flex; align-items: center; justify-content: center; gap: 5px;
    min-height: 34px; padding: 5px 9px;
    font: 600 .76rem/1.2 'Segoe UI', Tahoma, 'Sarabun', sans-serif;
    color: var(--primary); background: var(--surface);
    border: 1px solid var(--border); border-radius: 7px; cursor: pointer;
    transition: color .15s, background .15s, border-color .15s;
}
.copy-btn:hover { border-color: var(--primary); background: color-mix(in srgb, var(--primary) 8%, var(--surface)); }
.copy-btn.is-copied { color: var(--success); border-color: var(--success); background: var(--success-bg); }
.copy-btn.is-error { color: var(--danger); border-color: var(--danger); background: var(--danger-bg); }
.license__meta { display: flex; flex-wrap: wrap; gap: 8px 16px; font-size: .82rem; color: var(--text-soft); }
.license__actions { display: flex; gap: 8px; margin-top: auto; padding-top: 6px; }

/* ---------- Badges ---------- */
.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: .75rem; font-weight: 700; white-space: nowrap; }
.badge--type   { background: color-mix(in srgb, var(--primary) 15%, transparent); color: var(--primary); }
.badge--active { background: var(--success-bg); color: var(--success); }
.badge--soon   { background: var(--warning-bg); color: var(--warning); }
.badge--expired{ background: var(--danger-bg);  color: var(--danger); }

/* ---------- Empty state ---------- */
.empty { text-align: center; padding: 60px 20px; color: var(--text-soft); }
.empty__icon { font-size: 3rem; }
.empty h2 { margin: 12px 0 6px; color: var(--text); }

/* ---------- Admin tables ---------- */
.table-wrap { max-width: 100%; overflow-x: auto; overscroll-behavior-inline: contain; -webkit-overflow-scrolling: touch; }
.table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.table th, .table td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table th { color: var(--text-soft); font-weight: 600; font-size: .8rem; text-transform: uppercase; letter-spacing: .3px; white-space: nowrap; }
.table tbody tr:hover { background: var(--surface-2); }
.table .num { text-align: right; white-space: nowrap; }
.table form { display: inline; margin: 0; }
.row-actions { display: flex; gap: 6px; }
.mono { font-family: 'Consolas', ui-monospace, monospace; background: var(--surface-2); padding: 2px 6px; border-radius: 5px; font-size: .85rem; word-break: break-all; }

.badge--admin { background: color-mix(in srgb, var(--danger) 18%, transparent); color: var(--danger); }

/* ---------- Code blocks (API docs) ---------- */
pre.code {
    background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px;
    padding: 14px 16px; overflow-x: auto; margin: 8px 0 18px;
}
pre.code code { font-family: 'Consolas', ui-monospace, monospace; font-size: .85rem; color: var(--text); white-space: pre; }
.card h3 { margin: 22px 0 6px; }

.muted { color: var(--text-soft); }
.text-center { text-align: center; }
a { color: var(--primary); }

/* =========================================================================
   Small screens / touch devices
   ========================================================================= */
@media (max-width: 480px) {
    .layout--guest .main--center {
        min-height: auto;
        justify-content: flex-start;
        gap: 12px;
        padding-top: max(24px, env(safe-area-inset-top));
    }

    .auth-brand { font-size: 1.2rem; }
    .auth-wrap { margin: 8px auto 0; }
    .auth-wrap h1 { font-size: 1.65rem; margin-bottom: 6px; }

    .main {
        padding-top: 16px;
        padding-left: max(12px, env(safe-area-inset-left));
        padding-right: max(12px, env(safe-area-inset-right));
    }

    .card { padding: 16px; border-radius: 10px; }
    .card + .card { margin-top: 14px; }

    .page-head { display: block; margin-bottom: 18px; }
    .page-head h1 { font-size: 1.35rem; line-height: 1.35; }
    .page-head h2 { font-size: 1.15rem; line-height: 1.4; }
    .page-head p { font-size: .9rem; }
    .page-head > .btn { width: 100%; margin-top: 12px; }

    .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-bottom: 20px; }
    .stat { padding: 14px; min-width: 0; }
    .stat__num { font-size: 1.55rem; }
    .stat__label { line-height: 1.35; }

    .form-row { display: block; }
    .form-row .form-group {
        width: 100%; min-width: 0; max-width: none !important; margin-bottom: 14px !important;
    }

    /* 16px prevents automatic zoom when focusing fields on iOS Safari. */
    input, select, textarea { min-height: 44px; font-size: 16px; }
    textarea { min-height: 96px; }

    .input-with-btn { flex-direction: column; }
    .input-with-btn .btn { width: 100%; }

    .btn { min-height: 44px; padding: 9px 14px; }
    .btn--sm { min-height: 40px; padding: 7px 11px; }
    .btn-row { flex-direction: column; }
    .btn-row .btn { width: 100%; }

    .toolbar { gap: 8px; }
    .toolbar form { min-width: 0; width: 100%; flex-wrap: wrap; }
    .toolbar form input { flex: 1 0 100%; }
    .toolbar form .btn { flex: 1 1 0; }

    .license-grid { grid-template-columns: minmax(0, 1fr); gap: 12px; }
    .license { padding: 15px; min-width: 0; }
    .license__head { flex-wrap: wrap; }
    .license__head > div { min-width: 0; }
    .license__actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .license__actions .btn,
    .license__actions form,
    .license__actions form .btn { width: 100%; }
    .license__key { flex-direction: column; }
    .copy-btn { width: 100%; min-height: 40px; }

    .table-wrap { margin-inline: -16px; padding-inline: 16px; }
    .table:not(.device-table) { min-width: 680px; }
    .table th, .table td { padding: 9px 10px; }
    .row-actions { flex-wrap: nowrap; }
    .row-actions .btn { white-space: nowrap; }

    pre.code { margin-inline: -8px; padding: 12px; font-size: .8rem; }
    .empty { padding: 36px 14px; }
    .empty__icon { font-size: 2.4rem; }

    .flash { padding: 10px 12px; font-size: .9rem; overflow-wrap: anywhere; }
    .appfooter { padding: 12px max(12px, env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left)); }
}

@media (max-width: 360px) {
    .stats { grid-template-columns: minmax(0, 1fr); }
    .mobilebar__title { font-size: .92rem; }
    .license__actions { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 420px) {
    .device-table td { grid-template-columns: minmax(0, 1fr); gap: 4px; }
    .device-table td::before { font-size: .72rem; }
}

@media (hover: none) and (pointer: coarse) {
    .side-link { min-height: 44px; }
    .icon-btn { width: 44px; height: 44px; }
}
