:root {
    --bg: #f6f5f2;
    --panel: #ffffff;
    --ink: #1d1b18;
    --muted: #6b665e;
    --line: #e4e0d8;
    --accent: #d9480f;
    --accent-soft: #fff1e8;
    --danger: #c92a2a;
    --ok: #2b8a3e;
    --radius: 8px;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    font-size: 14px;
    color: var(--ink);
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0 0 16px; }
h2 { font-size: 16px; margin: 24px 0 12px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* Layout */
body.with-nav { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar {
    background: #1d1b18; color: #fff; padding: 20px 12px;
    display: flex; flex-direction: column; gap: 2px; position: sticky; top: 0; height: 100vh;
}
.sidebar a { color: #d8d3ca; padding: 8px 10px; border-radius: 6px; }
.sidebar a:hover { background: #2e2b27; text-decoration: none; color: #fff; }
.sidebar a.active { background: var(--accent); color: #fff; }
.sidebar .brand { font-size: 18px; font-weight: 700; color: #fff; margin-bottom: 16px; }
.sidebar .brand span { font-weight: 400; color: #a39d92; margin-left: 6px; font-size: 13px; }
.sidebar-foot { margin-top: auto; border-top: 1px solid #3a3631; padding-top: 12px; display: flex; flex-direction: column; gap: 4px; }
.sidebar-foot .link { color: #a39d92; padding: 8px 10px; text-align: left; }
.content { padding: 28px 32px; max-width: 1200px; width: 100%; }
body.bare .content { max-width: 380px; margin: 10vh auto; }

@media (max-width: 760px) {
    body.with-nav { grid-template-columns: 1fr; }
    .sidebar { position: static; height: auto; flex-direction: row; flex-wrap: wrap; }
    .sidebar-foot { margin-top: 0; border: 0; padding: 0; flex-direction: row; }
    .content { padding: 20px 16px; }
}

/* Panels, tables */
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; }
.toolbar { display: flex; gap: 12px; align-items: center; justify-content: space-between; margin-bottom: 16px; flex-wrap: wrap; }
.toolbar h1 { margin: 0; }
table { width: 100%; border-collapse: collapse; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); background: #faf9f6; }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

/* Stat tiles */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 8px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.stat .value { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .label { color: var(--muted); font-size: 12px; margin-top: 4px; }

/* Forms */
form.stack { display: flex; flex-direction: column; gap: 14px; }
label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 13px; }
label.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 500; }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=date],
input[type=datetime-local], input[type=search], select, textarea {
    font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; background: #fff; color: var(--ink);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
textarea { min-height: 120px; font-family: ui-monospace, monospace; font-size: 12px; }
.row { display: flex; gap: 12px; flex-wrap: wrap; }
.row > * { flex: 1 1 160px; }
.field-error, .validation-summary-errors { color: var(--danger); font-size: 12px; font-weight: 500; }

.btn, button.btn {
    display: inline-flex; align-items: center; gap: 6px; font: inherit; font-weight: 600;
    padding: 8px 14px; border-radius: 6px; border: 1px solid var(--accent); background: var(--accent); color: #fff; cursor: pointer;
}
.btn:hover { text-decoration: none; filter: brightness(1.05); }
.btn.secondary { background: #fff; color: var(--ink); border-color: var(--line); }
.btn.danger { background: #fff; color: var(--danger); border-color: #f1c4c4; }
.btn.small { padding: 4px 10px; font-size: 12px; }
button.link { background: none; border: 0; color: var(--accent); font: inherit; cursor: pointer; padding: 0; }
form.inline { display: inline; }

.flash { background: #ebfbee; border: 1px solid #b2f2bb; color: #1b5e20; padding: 10px 14px; border-radius: 6px; margin-bottom: 16px; }
.flash.error { background: #fff5f5; border-color: #ffc9c9; color: var(--danger); }

/* Badges */
.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 99px; background: #eee; color: #444; }
.badge.premium { background: #fff3bf; color: #8a5a00; }
.badge.off { background: #f1f0ec; color: #8e877c; }
.badge.ok { background: #ebfbee; color: var(--ok); }
.badge.warn { background: #fff5f5; color: var(--danger); }

/* Template grid */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }
.tile { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column; }
.tile img { width: 100%; aspect-ratio: 1; object-fit: cover; background: #eee; display: block; }
.tile.inactive img { opacity: .4; }
.tile .meta { padding: 8px 10px; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: 12px; }
.tile .actions { padding: 0 10px 10px; display: flex; gap: 6px; flex-wrap: wrap; }

.pager { display: flex; gap: 8px; margin-top: 16px; align-items: center; }

/* Frame editor */
.frame-editor { display: grid; grid-template-columns: minmax(300px, 540px) 1fr; gap: 20px; align-items: start; }
@media (max-width: 1000px) { .frame-editor { grid-template-columns: 1fr; } }
.canvas-wrap { position: relative; width: 100%; aspect-ratio: 1; border-radius: var(--radius); overflow: hidden; border: 1px solid var(--line); background: #ccc; }
.canvas-wrap canvas { width: 100%; height: 100%; display: block; touch-action: none; cursor: move; }
.slot-list { display: flex; flex-direction: column; gap: 8px; }
.slot-card { border: 1px solid var(--line); border-radius: 6px; padding: 10px; background: #fff; }
.slot-card.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.slot-card .head { display: flex; justify-content: space-between; align-items: center; font-weight: 600; margin-bottom: 8px; cursor: pointer; }
.slot-card .fields { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.slot-card .fields label { font-size: 11px; font-weight: 500; gap: 2px; }
.slot-card .fields input, .slot-card .fields select { padding: 4px 6px; font-size: 12px; width: 100%; }
.thumb-frame { width: 72px; height: 72px; border-radius: 6px; border: 1px solid var(--line); background: repeating-conic-gradient(#ddd 0 25%, #fff 0 50%) 0 0 / 12px 12px; object-fit: contain; }
.toolbar .row > * { flex: 0 0 auto; }
.toolbar form.row > input[type=search] { flex: 1 1 200px; }

/* Frames list: big square cards, each drawn with frame-render.js */
.frame-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; }
.frame-card canvas { width: 100%; aspect-ratio: 1; display: block; background: #eee; }
.frame-card.inactive canvas { opacity: .45; }
.frame-card .meta strong { font-size: 14px; margin-right: 2px; }
