:root {
    --red: #c00d0d;
    --red-dark: #9a0b0b;
    --red-soft: #fdf1f0;
    --red-line: #f5c9c6;
    --ink: #1c1917;
    --ink-2: #44403c;
    --muted: #8a8580;
    --line: #eae7e4;
    --line-soft: #f1efec;
    --bg: #f7f6f4;
    --card: #ffffff;
    --ok: #157f3d;
    --ok-soft: #eafaf0;
    --warn: #b45309;
    --warn-soft: #fff8e6;
    --radius: 14px;
    --shadow-sm: 0 1px 2px rgba(28, 25, 23, .05);
    --shadow-md: 0 2px 10px rgba(28, 25, 23, .06);
    --shadow-lg: 0 24px 70px rgba(28, 25, 23, .22);
}

* { box-sizing: border-box; }
::selection { background: #f6d5d3; }

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

body {
    margin: 0;
    font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    font-size: 14.5px;
    line-height: 1.55;
    color: var(--ink);
    background: var(--bg);
}

h1 { font-size: 22px; font-weight: 700; letter-spacing: -.02em; margin: 0; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
h2 { font-size: 12px; font-weight: 600; margin: 0 0 14px; color: var(--muted); text-transform: uppercase; letter-spacing: .07em; }
a { color: var(--red); text-decoration: none; }
a:hover { text-decoration: underline; }
code {
    background: var(--line-soft);
    border-radius: 6px;
    padding: 2px 6px;
    font-family: ui-monospace, "SF Mono", Menlo, monospace;
    font-size: 12.5px;
    word-break: break-all;
}
.muted { color: var(--muted); }

/* ================= admin shell ================= */
body.admin { display: flex; min-height: 100vh; min-height: 100dvh; }

.sidebar {
    width: 232px;
    flex-shrink: 0;
    background: var(--card);
    border-right: 1px solid var(--line);
    display: flex;
    flex-direction: column;
    padding: 22px 14px 18px;
    position: sticky;
    top: 0;
    height: 100vh;
    height: 100dvh;
}
.sidebar .brand { margin: 2px 10px 26px; }
.sidebar .brand img { height: 24px; display: block; }
.sidebar nav { display: flex; flex-direction: column; gap: 3px; }
.sidebar nav a {
    padding: 10px 12px;
    border-radius: 10px;
    color: var(--ink-2);
    font-weight: 500;
    transition: background .12s, color .12s;
}
.sidebar nav a:hover { background: var(--bg); text-decoration: none; }
.sidebar nav a.active { background: var(--red-soft); color: var(--red); font-weight: 600; }
.sidebar-foot {
    margin-top: auto;
    font-size: 13px;
    color: var(--muted);
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    padding: 12px 10px 0;
    border-top: 1px solid var(--line-soft);
}
.sidebar-foot span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-foot a { font-weight: 500; flex-shrink: 0; }

.main { flex: 1; padding: 30px 36px 60px; max-width: 1080px; min-width: 0; }

.page-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head h1 { margin: 0; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.main > h1:first-child { margin-bottom: 20px; }
.main > .muted { margin-top: -12px; margin-bottom: 18px; font-size: 13px; overflow-wrap: anywhere; }

/* ================= cards & stats ================= */
.card {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 22px;
    margin-bottom: 16px;
    box-shadow: var(--shadow-sm);
}
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 14px; margin-bottom: 16px; }
.stat-grid .card { margin-bottom: 0; }
.stat { display: flex; flex-direction: column; gap: 4px; justify-content: center; }
.stat-num { font-size: 30px; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.stat-label { color: var(--muted); font-size: 12.5px; font-weight: 500; }

/* ================= forms ================= */
.field { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; font-weight: 500; color: var(--ink-2); }
.field.wide { flex: 1 1 280px; }
.field input, .field select {
    padding: 10px 12px;
    border: 1px solid var(--line);
    border-radius: 10px;
    font: inherit;
    color: var(--ink);
    background: #fff;
    min-width: 0;
    width: 100%;
    transition: border-color .12s, box-shadow .12s;
}
.field input:focus {
    outline: none;
    border-color: var(--red);
    box-shadow: 0 0 0 3px rgba(192, 13, 13, .12);
}
.form-row { display: flex; flex-wrap: wrap; gap: 14px; align-items: flex-end; }
.form-row .field { flex: 1 1 200px; }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    background: var(--red);
    color: #fff;
    border: 0;
    border-radius: 10px;
    padding: 10px 18px;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    box-shadow: var(--shadow-sm);
    transition: background .12s, transform .06s;
    -webkit-tap-highlight-color: transparent;
}
.btn:hover { background: var(--red-dark); text-decoration: none; color: #fff; }
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: 3px solid rgba(192, 13, 13, .3); outline-offset: 1px; }
.btn-sm { padding: 6px 12px; font-size: 12.5px; border-radius: 8px; }
.btn-danger { background: #fff; color: var(--red); border: 1px solid var(--red-line); box-shadow: none; }
.btn-danger:hover { background: var(--red-soft); color: var(--red); }
.btn-ghost { background: #fff; color: var(--ink-2); border: 1px solid var(--line); box-shadow: none; }
.btn-ghost:hover { background: var(--bg); color: var(--ink); }

/* ================= tables ================= */
.table { display: block; width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; border-collapse: collapse; font-size: 13.5px; }
.table tbody { display: table; width: 100%; border-collapse: collapse; }
.table td, .table th { white-space: nowrap; }
.table td code { display: inline-block; max-width: 190px; overflow: hidden; text-overflow: ellipsis; vertical-align: middle; word-break: normal; }
.table th {
    text-align: left;
    color: var(--muted);
    font-weight: 600;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: .06em;
    padding: 0 10px 10px;
    border-bottom: 1px solid var(--line);
    white-space: nowrap;
}
.table td { padding: 11px 10px; border-bottom: 1px solid var(--line-soft); vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table tbody tr, .table tr { transition: background .1s; }
.table tr:hover td { background: #fcfbfa; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table td form { margin: 0; display: inline; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }
.pos { color: var(--ok); font-weight: 600; }
.neg { color: var(--red); font-weight: 600; }

.badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 99px;
    font-size: 11.5px;
    font-weight: 600;
    background: var(--line-soft);
    color: var(--muted);
    vertical-align: middle;
}
.badge-topup, .badge-active { background: var(--ok-soft); color: var(--ok); }
.badge-usage { background: var(--red-soft); color: var(--red); }
.badge-refund, .badge-adjustment { background: var(--warn-soft); color: var(--warn); }

.flash { padding: 13px 16px; border-radius: 12px; margin-bottom: 16px; font-size: 13.5px; }
.flash-ok { background: var(--ok-soft); color: var(--ok); border: 1px solid #bfe8cd; }
.flash-error { background: var(--red-soft); color: var(--red); border: 1px solid var(--red-line); }
.flash .token { display: block; margin-top: 8px; font-size: 13px; padding: 10px; user-select: all; background: #fff; }

/* ================= modals ================= */
.modal {
    border: 0;
    border-radius: 16px;
    padding: 26px;
    width: calc(100vw - 32px);
    max-width: 420px;
    box-shadow: var(--shadow-lg);
    animation: modal-in .16s ease-out;
}
@keyframes modal-in {
    from { opacity: 0; transform: translateY(10px) scale(.98); }
    to { opacity: 1; transform: none; }
}
.modal::backdrop { background: rgba(28, 25, 23, .45); backdrop-filter: blur(2px); }
.modal h2 { color: var(--ink); text-transform: none; font-size: 17px; font-weight: 700; letter-spacing: -.01em; margin-bottom: 4px; }
.modal form { display: flex; flex-direction: column; gap: 13px; }
.modal .muted { margin: 0; font-size: 13px; font-weight: 400; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }

/* ================= public pages ================= */
body.public {
    background:
        radial-gradient(900px 420px at 50% -10%, #fbe9e7 0%, rgba(251, 233, 231, 0) 70%) no-repeat,
        var(--bg);
    min-height: 100vh;
    min-height: 100dvh;
}
.center-wrap { min-height: 100vh; min-height: 100dvh; display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.auth-card {
    width: 100%;
    max-width: 400px;
    padding: 36px 32px;
    text-align: center;
    box-shadow: var(--shadow-md);
}
.auth-card .logo { height: 30px; margin-bottom: 18px; }
.auth-card h1 { justify-content: center; font-size: 19px; margin-bottom: 6px; }
.auth-card > .muted { font-size: 13.5px; margin: 0 0 10px; }
.auth-card form { text-align: left; display: flex; flex-direction: column; gap: 14px; margin-top: 16px; }
.auth-card .btn { width: 100%; padding: 12px; }

.packs { display: flex; gap: 10px; }
.pack {
    flex: 1;
    padding: 14px 6px;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 12px;
    font: inherit;
    font-size: 13px;
    cursor: pointer;
    color: var(--ink-2);
    line-height: 1.5;
    transition: border-color .12s, color .12s, box-shadow .12s;
}
.pack strong { display: block; font-size: 15px; }
.pack:hover { border-color: var(--red); color: var(--red); box-shadow: var(--shadow-sm); }

/* ================= embeddable widget ================= */
.widget { padding: 18px 16px; }
.widget-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 14px; }
.widget-head .stat-label { display: block; }
.widget-head .stat-num { font-size: 26px; }
.widget .table td, .widget .table th { padding-left: 6px; padding-right: 6px; }
.widget-foot { font-size: 11.5px; margin: 12px 0 0; text-align: center; }

/* ================= embed snippet (client page) ================= */
.embed { display: grid; grid-template-columns: minmax(0, 1.3fr) minmax(0, 1fr); gap: 20px; margin-top: 14px; }
.embed-step { font-size: 12.5px; font-weight: 600; color: var(--ink-2); margin: 0 0 6px; }
.embed pre { margin: 0 0 16px; padding: 12px 14px; background: var(--ink); border-radius: 10px; overflow-x: auto; }
.embed pre code { background: none; padding: 0; color: #f5f5f4; font-size: 12px; line-height: 1.6; white-space: pre; word-break: normal; }
.embed-preview iframe { min-height: 360px !important; }

.card-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 4px; }
.card-head h2 { margin: 0; }
.modal-wide { max-width: 720px; }
.agent-brief {
    width: 100%; height: 52vh; resize: vertical; padding: 12px 14px;
    font: 12px/1.6 ui-monospace, "SF Mono", Menlo, monospace;
    color: var(--ink); background: var(--bg); border: 1px solid var(--line); border-radius: 10px;
}

/* ================= mobile ================= */
@media (max-width: 800px) {
    body.admin { flex-direction: column; }

    .sidebar {
        width: 100%;
        height: auto;
        flex-direction: row;
        flex-wrap: wrap;
        align-items: center;
        gap: 6px 6px;
        padding: 12px 14px 6px;
        border-right: 0;
        border-bottom: 1px solid var(--line);
        z-index: 20;
    }
    .sidebar .brand { margin: 0 10px 0 0; }
    .sidebar .brand img { height: 20px; }
    .sidebar nav { order: 3; width: 100%; flex-direction: row; gap: 2px; overflow-x: auto; scrollbar-width: none; margin: 0 -4px; }
    .sidebar nav::-webkit-scrollbar { display: none; }
    .sidebar nav a { padding: 8px 11px; font-size: 13.5px; white-space: nowrap; }
    .sidebar-foot { margin: 0 0 0 auto; padding: 0; border: 0; }
    .sidebar-foot span { display: none; }

    .main { padding: 20px 16px 48px; }
    h1 { font-size: 20px; }
    .card { padding: 16px; }
    .stat-grid { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
    .stat-num { font-size: 24px; }

    .page-head .actions { width: 100%; }
    .page-head .actions > * { flex: 1; }
    .page-head .actions .btn { width: 100%; padding-left: 10px; padding-right: 10px; white-space: nowrap; }
    .modal { padding: 20px; }
    .embed { grid-template-columns: minmax(0, 1fr); }
    .auth-card { padding: 28px 20px; }
}
