:root {
    --bg: #0b0d10;
    --panel: #12161b;
    --panel-2: #171c22;
    --panel-hover: #1c2229;
    --line: #232a32;
    --line-light: #2d3742;
    --ink: #e6edf3;
    --ink-dim: #8b96a3;
    --ink-muted: #57606a;
    --accent: #4fd1a5;
    --accent-dim: #2e6b53;
    --accent-glow: rgba(79, 209, 165, 0.25);
    --warn: #e8b339;
    --warn-bg: rgba(232, 179, 57, 0.12);
    --alert: #ef6566;
    --alert-bg: rgba(239, 101, 102, 0.12);
    --info: #5bc0ff;
    --info-bg: rgba(91, 192, 255, 0.12);
}

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

body {
    background: var(--bg);
    color: var(--ink);
    font-family: "Inter", "Segoe UI", "Sarabun", -apple-system, BlinkMacSystemFont, sans-serif;
    font-size: 14px;
    line-height: 1.5;
    min-height: 100vh;
}

.mono {
    font-family: ui-monospace, "SF Mono", "Cascadia Code", Consolas, monospace;
}

/* ===== BUTTONS ===== */
button {
    cursor: pointer;
    border: none;
    border-radius: 8px;
    font-family: inherit;
    font-weight: 600;
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}

button:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    transform: none !important;
}

.primary {
    background: var(--accent);
    color: #05231a;
    padding: 10px 18px;
    font-size: 13.5px;
}
.primary:hover:not(:disabled) {
    filter: brightness(1.1);
    box-shadow: 0 0 15px var(--accent-glow);
    transform: translateY(-1px);
}

.secondary {
    background: var(--panel-2);
    border: 1px solid var(--line);
    color: var(--ink);
    padding: 9px 14px;
    font-size: 13px;
}
.secondary:hover:not(:disabled) {
    border-color: var(--accent);
    background: var(--panel-hover);
}

.danger {
    background: var(--alert);
    color: #fff;
    padding: 10px 18px;
}
.danger:hover:not(:disabled) {
    filter: brightness(1.1);
}

.link-btn {
    background: none;
    color: var(--ink-dim);
    font-size: 12px;
    padding: 6px 10px;
    text-decoration: underline;
    border-radius: 6px;
}
.link-btn:hover {
    color: var(--ink);
}

.primary.full {
    width: 100%;
}

/* ===== FORM CONTROLS ===== */
.fld {
    display: block;
    margin-bottom: 14px;
}
.fld span {
    display: block;
    font-size: 11px;
    color: var(--ink-dim);
    margin-bottom: 6px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-weight: 600;
}
textarea, input[type=text], input[type=number], input[type=password], select {
    width: 100%;
    background: var(--bg);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 10px 12px;
    color: var(--ink);
    font-size: 13.5px;
    font-family: inherit;
    outline: none;
    transition: border-color 0.15s, box-shadow 0.15s;
}
textarea:focus, input:focus, select:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(79, 209, 165, 0.15);
}
textarea {
    resize: vertical;
}

/* ===== LOGIN OVERLAY ===== */
.login-overlay {
    position: fixed;
    inset: 0;
    z-index: 999;
    background: radial-gradient(circle at 50% 30%, #17323a 0%, var(--bg) 65%);
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(8px);
}
.login-card {
    width: 390px;
    background: var(--panel);
    border: 1px solid var(--line-light);
    border-radius: 16px;
    padding: 32px 28px;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.7);
}
.login-card h1 {
    font-size: 22px;
    letter-spacing: 0.3px;
    margin-bottom: 6px;
    display: flex;
    align-items: center;
    gap: 8px;
}
.login-card .sub {
    color: var(--ink-dim);
    font-size: 13px;
    margin-bottom: 22px;
}

.login-tabs {
    display: flex;
    gap: 8px;
    margin-bottom: 20px;
    background: var(--bg);
    padding: 4px;
    border-radius: 10px;
    border: 1px solid var(--line);
}
.ltab {
    flex: 1;
    padding: 9px;
    font-size: 12.5px;
    border-radius: 7px;
    background: transparent;
    color: var(--ink-dim);
    border: none;
}
.ltab.active {
    background: var(--panel-2);
    color: var(--accent);
    font-weight: 700;
    box-shadow: 0 2px 6px rgba(0,0,0,0.3);
}

.qr-box {
    width: 210px;
    height: 210px;
    margin: 0 auto;
    background: #fff;
    border-radius: 12px;
    padding: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 14px rgba(0,0,0,0.4);
}
.qr-box img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}
.qr-load {
    color: #666;
    font-size: 12.5px;
}
.pin-box {
    margin: 16px auto 0;
    padding: 12px;
    text-align: center;
    background: var(--warn-bg);
    border: 1px solid var(--warn);
    border-radius: 10px;
}
.pin {
    font-size: 28px;
    font-weight: 700;
    color: var(--warn);
    letter-spacing: 6px;
}
.qr-status-txt {
    text-align: center;
    font-size: 12px;
    color: var(--ink-dim);
    margin-top: 12px;
}
.err-msg {
    color: var(--alert);
    font-size: 12px;
    margin-top: 10px;
    min-height: 16px;
}

/* ===== TOP BAR ===== */
.topbar {
    display: none;
    align-items: center;
    justify-content: space-between;
    padding: 12px 24px;
    border-bottom: 1px solid var(--line);
    background: var(--panel);
    position: sticky;
    top: 0;
    z-index: 100;
}
.topbar-brand {
    display: flex;
    align-items: center;
    gap: 12px;
}
.topbar-brand h1 {
    font-size: 17px;
    font-weight: 700;
    letter-spacing: 0.2px;
}
.topbar-brand .badge-cf {
    font-size: 10.5px;
    font-weight: 700;
    padding: 2px 7px;
    border-radius: 6px;
    background: var(--accent);
    color: #05231a;
}
.mid-badge {
    font-size: 11px;
    color: var(--ink-dim);
    background: var(--panel-2);
    padding: 4px 9px;
    border-radius: 6px;
    border: 1px solid var(--line);
}

.badge {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    padding: 6px 14px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
}
.badge .dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
}
.badge.stop {
    background: var(--alert-bg);
    color: var(--alert);
}
.badge.stop .dot {
    background: var(--alert);
}
.badge.run {
    background: rgba(79, 209, 165, 0.12);
    color: var(--accent);
}
.badge.run .dot {
    background: var(--accent);
    animation: pulse 1.5s infinite;
}
@keyframes pulse {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.4; transform: scale(0.85); }
}

/* ===== APP NAVIGATION TABS ===== */
.app-nav {
    display: none;
    background: var(--panel-2);
    border-bottom: 1px solid var(--line);
    padding: 0 24px;
    gap: 6px;
    overflow-x: auto;
}
.app-nav.show {
    display: flex;
}
.nav-tab {
    padding: 12px 16px;
    color: var(--ink-dim);
    font-size: 13px;
    border-radius: 0;
    background: transparent;
    border-bottom: 2px solid transparent;
    white-space: nowrap;
}
.nav-tab:hover {
    color: var(--ink);
}
.nav-tab.active {
    color: var(--accent);
    border-bottom-color: var(--accent);
    font-weight: 600;
}

/* ===== MAIN CONTENT ===== */
.main-wrapper {
    display: none;
    max-width: 1280px;
    margin: 0 auto;
    padding: 22px;
}
.main-wrapper.show {
    display: block;
}

.tab-pane {
    display: none;
}
.tab-pane.active {
    display: block;
}

/* GRID LAYOUTS */
.grid-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 18px;
}
@media (max-width: 900px) {
    .grid-2 { grid-template-columns: 1fr; }
}

.col {
    display: flex;
    flex-direction: column;
    gap: 18px;
}
.panel {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 20px;
}
.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}
.panel h2 {
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 0.6px;
    color: var(--ink-dim);
    font-weight: 700;
}

/* STATS */
.stat-row {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
    margin-bottom: 16px;
}
.stat {
    background: var(--panel-2);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 14px 10px;
    text-align: center;
}
.stat .n {
    font-size: 24px;
    font-weight: 700;
}
.stat .l {
    font-size: 11px;
    color: var(--ink-dim);
    margin-top: 4px;
}
.stat.detected .n { color: var(--accent); }
.stat.replied .n { color: var(--info); }
.stat.failed .n { color: var(--alert); }
.stat.conn .n { color: var(--warn); }

.btn-row {
    display: flex;
    gap: 10px;
    margin-bottom: 14px;
}
.btn-row button {
    flex: 1;
}

.interval-row {
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--panel-2);
    padding: 10px 14px;
    border-radius: 8px;
    border: 1px solid var(--line);
}
input[type=range] {
    flex: 1;
    accent-color: var(--accent);
}
.interval-val {
    font-size: 12.5px;
    color: var(--accent);
    min-width: 55px;
    text-align: right;
}

/* ROOMS */
.row-actions {
    display: flex;
    gap: 8px;
    margin-bottom: 12px;
}
.room-list {
    max-height: 280px;
    overflow-y: auto;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel-2);
}
.room-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-bottom: 1px solid var(--line);
    font-size: 13.5px;
    cursor: pointer;
}
.room-item:hover {
    background: rgba(255, 255, 255, 0.02);
}
.room-item:last-child {
    border-bottom: none;
}
.room-item input[type=checkbox] {
    accent-color: var(--accent);
    width: 17px;
    height: 17px;
}
.room-item .name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.room-item .tag {
    font-size: 10px;
    padding: 2px 7px;
    border-radius: 6px;
    background: var(--alert-bg);
    color: var(--alert);
}
.room-empty, .trig-empty {
    color: var(--ink-dim);
    font-size: 12.5px;
    padding: 24px;
    text-align: center;
}

/* TRIGGERS */
.trig-form {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-bottom: 16px;
    background: var(--panel-2);
    padding: 14px;
    border-radius: 10px;
    border: 1px solid var(--line);
}
.trig-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-height: 280px;
    overflow-y: auto;
}
.trig-card {
    background: var(--panel-2);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 12px 14px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
}
.trig-card .words {
    font-size: 13px;
    font-weight: 600;
    color: var(--accent);
}
.trig-card .reply {
    font-size: 12px;
    color: var(--ink-dim);
    margin-top: 3px;
}
.trig-card .scope {
    font-size: 10.5px;
    color: var(--warn);
    margin-top: 3px;
}

/* DATA TABLES (PRODUCTS & ORDERS) */
.table-container {
    overflow-x: auto;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: var(--panel-2);
}
table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
    text-align: left;
}
th {
    background: var(--panel);
    color: var(--ink-dim);
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: 12px 14px;
    border-bottom: 1px solid var(--line);
}
td {
    padding: 12px 14px;
    border-bottom: 1px solid var(--line);
    vertical-align: middle;
}
tr:last-child td {
    border-bottom: none;
}
tr:hover td {
    background: rgba(255, 255, 255, 0.02);
}

.code-pill {
    display: inline-block;
    padding: 3px 8px;
    background: rgba(79, 209, 165, 0.15);
    color: var(--accent);
    border-radius: 6px;
    font-weight: 700;
    font-size: 12px;
}
.status-pill {
    display: inline-block;
    padding: 3px 9px;
    border-radius: 12px;
    font-size: 11px;
    font-weight: 600;
}
.status-pill.confirmed { background: var(--info-bg); color: var(--info); }
.status-pill.paid { background: rgba(79, 209, 165, 0.15); color: var(--accent); }
.status-pill.cancelled { background: var(--alert-bg); color: var(--alert); }

/* SYSTEM LOGS */
.logs {
    background: #07090b;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 14px;
    height: 420px;
    overflow-y: auto;
    font-size: 12.5px;
}
.log-line {
    padding: 2px 0;
    color: var(--ink-dim);
    white-space: pre-wrap;
    word-break: break-word;
    font-family: ui-monospace, Consolas, monospace;
}
.log-line.hit { color: var(--accent); }
.log-line.err { color: var(--alert); }
.log-line.info { color: var(--info); }
.log-line.warn { color: var(--warn); }

/* MODAL */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    z-index: 500;
    display: none;
    align-items: center;
    justify-content: center;
}
.modal-overlay.show {
    display: flex;
}
.modal-card {
    background: var(--panel);
    border: 1px solid var(--line-light);
    border-radius: 16px;
    padding: 26px;
    width: 440px;
    max-width: 90vw;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.6);
}
.modal-card h3 {
    font-size: 18px;
    margin-bottom: 18px;
}

/* TOAST */
.toast {
    position: fixed;
    bottom: 24px;
    right: 24px;
    padding: 12px 20px;
    border-radius: 10px;
    font-size: 13.5px;
    font-weight: 600;
    z-index: 9999;
    opacity: 0;
    transform: translateY(15px);
    transition: all 0.25s cubic-bezier(0.16, 1, 0.3, 1);
    pointer-events: none;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}
.toast.show {
    opacity: 1;
    transform: translateY(0);
}
.toast.ok { background: var(--accent); color: #05231a; }
.toast.err { background: var(--alert); color: #fff; }
.toast.inf { background: var(--info); color: #05202b; }

/* SIMULATOR CHAT */
.chat-sim-container {
    background: var(--panel-2);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 16px;
    display: flex;
    flex-direction: column;
    height: 480px;
}
.chat-feed {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-bottom: 16px;
    padding-right: 6px;
}
.chat-bubble {
    max-width: 80%;
    padding: 10px 14px;
    border-radius: 14px;
    font-size: 13px;
    line-height: 1.4;
}
.chat-bubble.user {
    align-self: flex-end;
    background: #1b3842;
    border: 1px solid var(--accent-dim);
    color: #e6edf3;
    border-bottom-right-radius: 2px;
}
.chat-bubble.bot {
    align-self: flex-start;
    background: #1c2229;
    border: 1px solid var(--line);
    color: #e6edf3;
    border-bottom-left-radius: 2px;
}
.chat-sender {
    font-size: 10.5px;
    color: var(--ink-dim);
    margin-bottom: 3px;
}

::-webkit-scrollbar {
    width: 7px;
    height: 7px;
}
::-webkit-scrollbar-track {
    background: transparent;
}
::-webkit-scrollbar-thumb {
    background: var(--line);
    border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
    background: var(--line-light);
}

/* ===== LICENSE BADGE & ADMIN LINK ===== */
.badge-license {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 11.5px;
    font-weight: 600;
    background: rgba(79, 209, 165, 0.12);
    border: 1px solid rgba(79, 209, 165, 0.3);
    color: var(--accent);
    cursor: pointer;
    transition: all 0.2s ease;
}
.badge-license:hover {
    background: rgba(79, 209, 165, 0.22);
    box-shadow: 0 0 12px var(--accent-glow);
}
.badge-license.invalid {
    background: rgba(239, 101, 102, 0.12);
    border-color: rgba(239, 101, 102, 0.3);
    color: var(--alert);
}
.admin-link-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    border-radius: 8px;
    font-size: 12px;
    font-weight: 600;
    color: #d8b4fe;
    background: rgba(168, 85, 247, 0.12);
    border: 1px solid rgba(168, 85, 247, 0.25);
    text-decoration: none;
    transition: all 0.2s ease;
}
.admin-link-btn:hover {
    background: rgba(168, 85, 247, 0.25);
    border-color: rgba(168, 85, 247, 0.45);
    color: #ffffff;
    box-shadow: 0 0 14px rgba(168, 85, 247, 0.3);
}

