/* Stream Box — UI v2
   ธีม: มืดเป็นค่าเริ่มต้น สลับสว่างได้ด้วย [data-theme="light"] บน <html>
   ไม่พึ่งไลบรารีภายนอก */

:root {
    --bg: #0b0d12;
    --surface: #12151c;
    --surface-2: #191d27;
    --field: #0b0d12;
    --border: #252b38;
    --border-strong: #343c4d;
    --text: #e7eaf0;
    --muted: #8f98a9;
    --accent: #34d3a6;
    --accent-hover: #4fe0b6;
    --accent-ink: #04231a;
    --accent-soft: rgba(52, 211, 166, .12);
    --danger: #ff7575;
    --danger-soft: rgba(255, 117, 117, .12);
    --warn: #f5b84b;
    --warn-soft: rgba(245, 184, 75, .12);
    --shadow: 0 1px 0 rgba(255, 255, 255, .03) inset, 0 8px 24px rgba(0, 0, 0, .25);
    --radius: 14px;
    --radius-sm: 9px;
    --sidebar-w: 256px;
    --font: "IBM Plex Sans Thai", "Noto Sans Thai", "Leelawadee UI", "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
    --mono: ui-monospace, "Cascadia Code", "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
    color-scheme: dark;
}

[data-theme="light"] {
    --bg: #f3f5f8;
    --surface: #ffffff;
    --surface-2: #eef1f5;
    --field: #ffffff;
    --border: #dde2ea;
    --border-strong: #c5ccd8;
    --text: #141821;
    --muted: #5b6474;
    --accent: #0b7a5c;
    --accent-hover: #096a50;
    --accent-ink: #ffffff;
    --accent-soft: rgba(11, 122, 92, .10);
    --danger: #c43131;
    --danger-soft: rgba(196, 49, 49, .08);
    --warn: #8a5a00;
    --warn-soft: rgba(196, 136, 0, .12);
    --shadow: 0 1px 2px rgba(20, 24, 33, .05), 0 8px 24px rgba(20, 24, 33, .06);
    color-scheme: light;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }
body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    font-size: 15px;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
}

h1, h2, h3, p, ul, ol, fieldset, legend { margin: 0; padding: 0; }
ul, ol { list-style: none; }
fieldset { border: 0; min-width: 0; }
a { color: inherit; text-decoration: none; }
button, input, textarea, select { font: inherit; color: inherit; }
strong { font-weight: 600; }

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

.muted { color: var(--muted); font-weight: 400; }
.sr-only {
    position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0;
    overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.skip-link {
    position: absolute; left: 12px; top: -60px; z-index: 100;
    background: var(--accent); color: var(--accent-ink);
    padding: 8px 14px; border-radius: var(--radius-sm); font-weight: 600;
}
.skip-link:focus { top: 12px; }
.icon { flex: none; }

/* ---------- Layout ---------- */
.app { display: grid; grid-template-columns: var(--sidebar-w) minmax(0, 1fr); min-height: 100vh; }

.sidebar {
    position: sticky; top: 0; height: 100vh;
    display: flex; flex-direction: column; gap: 22px;
    padding: 20px 14px;
    background: var(--surface);
    border-right: 1px solid var(--border);
    overflow-y: auto;
}

.brand { display: flex; align-items: center; gap: 12px; padding: 4px 8px; }
.brand-mark {
    display: grid; place-items: center; width: 38px; height: 38px;
    border-radius: 11px; background: var(--accent); color: var(--accent-ink);
}
.brand-mark .icon { width: 20px; height: 20px; }
.brand-text { display: flex; flex-direction: column; line-height: 1.25; }
.brand-text strong { font-size: 16px; letter-spacing: .2px; }
.brand-text small { color: var(--muted); font-size: 12px; }

.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-label {
    margin: 18px 10px 6px; color: var(--muted);
    font-size: 11.5px; font-weight: 600; letter-spacing: .6px;
}
.nav-link {
    display: flex; align-items: center; gap: 11px;
    padding: 9px 11px; border-radius: var(--radius-sm);
    color: var(--muted); font-weight: 500;
    transition: background .15s, color .15s;
}
.nav-link:hover { background: var(--surface-2); color: var(--text); }
.nav-link.is-active { background: var(--accent-soft); color: var(--accent); }
.nav-sub { font-size: 14px; font-weight: 400; }

.sidebar-foot {
    margin-top: auto; padding: 14px 10px 4px;
    border-top: 1px solid var(--border);
    color: var(--muted); font-size: 12.5px;
    display: flex; flex-direction: column; gap: 10px; align-items: flex-start;
}

.main-col { display: flex; flex-direction: column; min-width: 0; }

.topbar {
    position: sticky; top: 0; z-index: 20;
    display: flex; align-items: center; gap: 12px;
    padding: 14px 28px;
    background: color-mix(in srgb, var(--bg) 86%, transparent);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--border);
}
.page-title { flex: 1; font-size: 19px; font-weight: 600; }

.content {
    flex: 1; width: 100%; max-width: 1240px; margin: 0 auto;
    padding: 24px 28px 40px;
    display: flex; flex-direction: column; gap: 18px;
}

.footer {
    display: flex; flex-wrap: wrap; justify-content: space-between; gap: 6px 20px;
    padding: 16px 28px; border-top: 1px solid var(--border);
    color: var(--muted); font-size: 12.5px;
}

.scrim { position: fixed; inset: 0; z-index: 30; background: rgba(0, 0, 0, .55); }
.icon-btn.menu-btn { display: none; }

/* ---------- Components ---------- */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 22px;
    box-shadow: var(--shadow);
}
.card-title { display: flex; align-items: center; gap: 10px; font-size: 16px; font-weight: 600; }
.card-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.card > .card-title { margin-bottom: 16px; }
.card > .section-gap { margin-top: 28px; padding-top: 22px; border-top: 1px solid var(--border); }

.step-no {
    display: grid; place-items: center; width: 24px; height: 24px;
    border-radius: 50%; background: var(--accent-soft); color: var(--accent);
    font-size: 13px; font-weight: 700;
}

.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    min-height: 40px; padding: 8px 16px;
    border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
    background: var(--surface-2); color: var(--text);
    font-weight: 500; white-space: nowrap; cursor: pointer;
    transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { border-color: var(--muted); }
.btn:active { transform: translateY(1px); }
.btn[disabled] { opacity: .6; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--surface-2); border-color: transparent; color: var(--text); }
.btn-lg { min-height: 48px; padding: 10px 22px; font-size: 16px; }
.btn-sm { min-height: 32px; padding: 4px 10px; font-size: 13px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 16px; }
.btn-row.center { justify-content: center; }

.icon-btn {
    display: grid; place-items: center; width: 40px; height: 40px;
    border: 1px solid var(--border); border-radius: var(--radius-sm);
    background: var(--surface); color: var(--muted); cursor: pointer;
}
.icon-btn:hover { color: var(--text); border-color: var(--border-strong); }
.when-light, [data-theme="light"] .when-dark { display: none; }
[data-theme="light"] .when-light, .when-dark { display: grid; }

.pill {
    display: inline-flex; align-items: center; padding: 2px 10px;
    border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border);
    color: var(--muted); font-size: 12.5px; font-weight: 500; white-space: nowrap;
}
.pill-ok { background: var(--accent-soft); border-color: transparent; color: var(--accent); }

.text-link { color: var(--accent); font-size: 13.5px; font-weight: 500; }
.text-link:hover { text-decoration: underline; }

.alert {
    margin-top: 12px; padding: 10px 14px; border-radius: var(--radius-sm);
    font-size: 14px; border: 1px solid transparent;
}
.alert-error { background: var(--danger-soft); color: var(--danger); border-color: color-mix(in srgb, var(--danger) 30%, transparent); }
.alert-warn { background: var(--warn-soft); color: var(--warn); margin: 0 0 14px; }

/* ---------- Forms ---------- */
.label { display: block; margin-bottom: 7px; font-size: 13.5px; font-weight: 500; }
.hint { margin-top: 7px; color: var(--muted); font-size: 13px; }
.hint:empty { display: none; }
.field { margin-top: 18px; }
.field-grid { display: grid; grid-template-columns: 120px minmax(0, 1fr); gap: 0 14px; }

.input {
    width: 100%; min-height: 42px; padding: 8px 13px;
    background: var(--field); border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
    transition: border-color .15s, box-shadow .15s;
}
.input::placeholder { color: var(--muted); opacity: .75; }
.input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.input:disabled { opacity: .5; cursor: not-allowed; }
.input-lg { min-height: 48px; font-size: 16px; }
.input-row { display: flex; gap: 10px; }
.input-row .input { flex: 1; min-width: 0; }

.check { display: inline-flex; align-items: center; gap: 9px; margin-top: 12px; cursor: pointer; font-size: 14px; }
.check input { width: 17px; height: 17px; accent-color: var(--accent); }

.segmented { display: flex; flex-wrap: wrap; gap: 8px; }
.segmented label { position: relative; cursor: pointer; }
.segmented input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.segmented span {
    display: block; padding: 7px 14px;
    border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
    background: var(--field); color: var(--muted); font-size: 14px; font-weight: 500;
    transition: background .15s, border-color .15s, color .15s;
}
.segmented label:hover span { color: var(--text); }
.segmented input:checked + span { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip {
    padding: 3px 11px; border-radius: 999px; cursor: pointer;
    background: transparent; border: 1px dashed var(--border-strong);
    color: var(--muted); font-size: 12.5px;
}
.chip:hover { color: var(--accent); border-color: var(--accent); }

.spinner {
    width: 15px; height: 15px; border-radius: 50%;
    border: 2px solid currentColor; border-right-color: transparent;
    animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Dashboard ---------- */
.hero {
    display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.1fr); gap: 24px; align-items: center;
    padding: 28px;
    background:
        radial-gradient(600px 220px at 100% 0%, var(--accent-soft), transparent 70%),
        var(--surface);
}
.hero h2 { font-size: 22px; line-height: 1.4; font-weight: 600; text-wrap: balance; }
.hero-text p { margin-top: 8px; }
.hero-form { display: flex; gap: 10px; }
.hero-form .input { flex: 1; min-width: 0; }

.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 18px; }
.stat { display: grid; grid-template-columns: 1fr; gap: 2px; padding: 18px 22px; }
.stat-label { color: var(--muted); font-size: 13px; }
.stat-value { font-size: 30px; font-weight: 600; line-height: 1.25; font-variant-numeric: tabular-nums; }
.stat-text { font-size: 18px; line-height: 2.08; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stat-unit { color: var(--muted); font-size: 12.5px; }

.format-list { display: flex; flex-direction: column; gap: 16px; }
.format-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 6px; }
.sample {
    display: block; padding: 10px 12px; border-radius: var(--radius-sm);
    background: var(--bg); border: 1px solid var(--border);
    font-family: var(--mono); font-size: 12.5px; color: var(--muted);
    overflow-wrap: anywhere;
}

.steps { counter-reset: step; display: flex; flex-direction: column; gap: 14px; }
.steps li { counter-increment: step; position: relative; padding-left: 40px; display: flex; flex-direction: column; }
.steps li::before {
    content: counter(step); position: absolute; left: 0; top: 1px;
    display: grid; place-items: center; width: 26px; height: 26px; border-radius: 50%;
    background: var(--accent-soft); color: var(--accent); font-size: 13px; font-weight: 700;
}
.steps span { color: var(--muted); font-size: 14px; }

.history { display: flex; flex-direction: column; }
.history li + li { border-top: 1px solid var(--border); }
.history a {
    display: grid; grid-template-columns: auto minmax(0, 1fr) auto auto; align-items: center; gap: 12px;
    padding: 11px 8px; border-radius: var(--radius-sm);
}
.history a:hover { background: var(--surface-2); }
.history .icon { color: var(--accent); }
.history-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.history-time { color: var(--muted); font-size: 13px; white-space: nowrap; }
.empty { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 14px; }

.empty-page { text-align: center; padding: 56px 22px; }
.empty-code { font-size: 64px; font-weight: 700; line-height: 1; color: var(--accent); margin-bottom: 10px; }
.empty-page h2 { font-size: 20px; margin-bottom: 6px; }

/* ---------- Generate ---------- */
.generate { display: grid; grid-template-columns: minmax(340px, 430px) minmax(0, 1fr); gap: 18px; align-items: stretch; }
.panel-result { display: flex; flex-direction: column; min-height: 520px; }
.result-title { display: flex; align-items: center; gap: 10px; min-width: 0; }
.result-title .card-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: block; }

.tabs { display: inline-flex; padding: 3px; border-radius: var(--radius-sm); background: var(--bg); border: 1px solid var(--border); }
.tab {
    padding: 5px 14px; border: 0; border-radius: 7px; background: transparent;
    color: var(--muted); font-size: 13.5px; font-weight: 500; cursor: pointer;
}
.tab.is-active { background: var(--surface-2); color: var(--text); }

.result-empty {
    flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
    padding: 40px 16px; text-align: center;
    border: 1px dashed var(--border-strong); border-radius: var(--radius-sm);
}
.result-empty .icon { width: 36px; height: 36px; color: var(--muted); }

.tab-pane { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.output {
    flex: 1; width: 100%; min-height: 400px; padding: 14px;
    background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius-sm);
    font-family: var(--mono); font-size: 12.5px; line-height: 1.65;
    resize: vertical; overflow-wrap: anywhere;
}
.output:focus { outline: none; border-color: var(--accent); }

.table-wrap { max-height: 520px; overflow: auto; border: 1px solid var(--border); border-radius: var(--radius-sm); }
.table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.table th, .table td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--border); }
.table th { position: sticky; top: 0; background: var(--surface-2); color: var(--muted); font-size: 12.5px; font-weight: 600; white-space: nowrap; }
.table tr:last-child td { border-bottom: 0; }
.table td { max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.table .col-no { width: 52px; color: var(--muted); font-variant-numeric: tabular-nums; }
.table .col-link { width: 70px; }
.table .dim { color: var(--muted); }

/* ---------- Toast ---------- */
.toast {
    position: fixed; left: 50%; bottom: 26px; z-index: 60; transform: translateX(-50%);
    padding: 10px 18px; border-radius: 999px;
    background: var(--text); color: var(--bg); font-size: 14px; font-weight: 500;
    box-shadow: 0 10px 30px rgba(0, 0, 0, .35);
}
.noscript { position: fixed; inset: auto 0 0 0; padding: 12px; text-align: center; background: var(--danger); color: #fff; }

/* ---------- Responsive ---------- */
@media (max-width: 1080px) {
    .generate { grid-template-columns: minmax(0, 1fr); }
    .panel-result { min-height: 0; }
    .hero { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 860px) {
    .app { grid-template-columns: minmax(0, 1fr); }
    .sidebar {
        position: fixed; inset: 0 auto 0 0; z-index: 40; width: min(var(--sidebar-w), 84vw);
        transform: translateX(-100%); transition: transform .2s ease; visibility: hidden;
    }
    .nav-open .sidebar { transform: none; visibility: visible; }
    .icon-btn.menu-btn { display: grid; }
    .topbar { padding: 12px 16px; }
    .content { padding: 16px 16px 32px; gap: 14px; }
    .footer { padding: 14px 16px; }
    .grid-2 { grid-template-columns: minmax(0, 1fr); gap: 14px; }
    .stats { gap: 14px; }
    .card { padding: 18px; }
    .hero { padding: 20px; }
}

@media (max-width: 600px) {
    .stats { grid-template-columns: minmax(0, 1fr); }
    .hero-form, .input-row { flex-direction: column; }
    .hero h2 { font-size: 19px; }
    .field-grid { grid-template-columns: minmax(0, 1fr); }
    .history a { grid-template-columns: auto minmax(0, 1fr) auto; }
    .history-time { display: none; }
    .output { min-height: 300px; }
    .table .col-orig { display: none; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { transition: none !important; animation-duration: 1.5s !important; }
}
