/* ==========================================================================
   RI-PAL — design system
   ========================================================================== */

:root {
    --bg: #f4f6fa;
    --surface: #ffffff;
    --surface-2: #f8f9fc;
    --surface-3: #eef1f6;
    --border: #e3e7ef;
    --border-strong: #cfd5e1;
    --text: #1b2232;
    --text-2: #4a5468;
    --muted: #7a8396;

    --primary: #4f56d8;
    --primary-hover: #4148c4;
    --primary-soft: #eceeff;
    --primary-ring: rgba(79, 86, 216, .22);

    --danger: #dc3d43;
    --danger-soft: #fdecec;
    --success: #1f9d63;
    --success-soft: #e6f6ee;
    --warning: #c77c02;
    --warning-soft: #fff5e0;

    --radius-sm: 8px;
    --radius: 12px;
    --radius-lg: 16px;
    --shadow-sm: 0 1px 2px rgba(16, 24, 40, .06);
    --shadow: 0 1px 3px rgba(16, 24, 40, .06), 0 4px 12px rgba(16, 24, 40, .05);
    --shadow-lg: 0 12px 32px rgba(16, 24, 40, .14);

    --sidebar-w: 252px;
    --font: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
    --mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
}

*, *::before, *::after { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
    font-family: var(--font);
    font-size: 14px;
    line-height: 1.5;
    color: var(--text);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
}

a { color: var(--primary); text-decoration: none; }
a:hover { color: var(--primary-hover); }

h1, h2, h3, h4, h5 { margin: 0; font-weight: 650; letter-spacing: -.01em; color: var(--text); }

.icon {
    font-family: 'Material Symbols Rounded';
    font-weight: normal;
    font-style: normal;
    font-size: 20px;
    line-height: 1;
    letter-spacing: normal;
    text-transform: none;
    display: inline-block;
    white-space: nowrap;
    direction: ltr;
    -webkit-font-smoothing: antialiased;
    font-variation-settings: 'FILL' 0, 'wght' 450, 'GRAD' 0, 'opsz' 24;
    user-select: none;
    flex-shrink: 0;
}
.icon.filled { font-variation-settings: 'FILL' 1, 'wght' 450, 'GRAD' 0, 'opsz' 24; }

/* ---------------------------------------------------------------- Layout */

.app-sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: var(--sidebar-w);
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    z-index: 40;
    transition: transform .2s ease;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 18px 20px;
    height: 64px;
    border-bottom: 1px solid var(--border);
}
.sidebar-brand img { height: 30px; width: auto; border-radius: 6px; }
.brand-mark {
    width: 30px; height: 30px;
    border-radius: 8px;
    background: linear-gradient(135deg, #5d64e6, #8b5cf6);
    color: #fff;
    display: grid; place-items: center;
}
.brand-mark .icon { font-size: 18px; }
.brand-name { font-weight: 700; font-size: 16px; letter-spacing: .02em; }

.sidebar-nav { flex: 1; overflow-y: auto; padding: 14px 12px; }
.nav-section {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--muted);
    padding: 14px 10px 6px;
}
.nav-link {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 9px 10px;
    margin: 1px 0;
    border-radius: var(--radius-sm);
    color: var(--text-2);
    font-weight: 500;
    transition: background .12s, color .12s;
}
.nav-link .icon { color: var(--muted); font-size: 20px; }
.nav-link:hover { background: var(--surface-3); color: var(--text); }
.nav-link.active { background: var(--primary-soft); color: var(--primary); }
.nav-link.active .icon { color: var(--primary); font-variation-settings: 'FILL' 1, 'wght' 500, 'GRAD' 0, 'opsz' 24; }

.sidebar-user {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px 16px;
    border-top: 1px solid var(--border);
}
.avatar {
    width: 34px; height: 34px;
    border-radius: 50%;
    background: var(--primary-soft);
    color: var(--primary);
    font-weight: 700;
    display: grid; place-items: center;
    text-transform: uppercase;
    flex-shrink: 0;
}
.sidebar-user .user-meta { flex: 1; min-width: 0; }
.sidebar-user .user-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-user .user-company { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-user form { margin: 0; }

.app-main {
    margin-left: var(--sidebar-w);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.app-topbar {
    display: none;
    align-items: center;
    gap: 12px;
    height: 56px;
    padding: 0 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 30;
}

.page { padding: 28px 32px 40px; width: 100%; max-width: 1240px; margin: 0 auto; }
.page.narrow { max-width: 760px; }

/* Full-height editor pages */
body.fullbleed .page { max-width: none; padding: 20px 24px; height: 100vh; display: flex; flex-direction: column; }

.sidebar-backdrop { display: none; }

@media (max-width: 992px) {
    .app-sidebar { transform: translateX(-100%); box-shadow: var(--shadow-lg); }
    body.sidebar-open .app-sidebar { transform: none; }
    body.sidebar-open .sidebar-backdrop {
        display: block; position: fixed; inset: 0; background: rgba(15, 20, 30, .35); z-index: 35;
    }
    .app-main { margin-left: 0; }
    .app-topbar { display: flex; }
    .page { padding: 20px 16px 32px; }
    body.fullbleed .page { height: calc(100vh - 56px); padding: 12px 16px; }
}

/* ----------------------------------------------------------- Page header */

.page-header {
    display: flex;
    align-items: center;
    gap: 14px;
    margin-bottom: 22px;
    flex-wrap: wrap;
}
.page-header .titles { flex: 1; min-width: 200px; }
.page-header h1 { font-size: 22px; line-height: 1.25; }
.page-header .subtitle { color: var(--muted); margin-top: 2px; }
.page-header .actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

.breadcrumb { display: flex; align-items: center; gap: 4px; font-size: 13px; color: var(--muted); margin-bottom: 4px; flex-wrap: wrap; }
.breadcrumb a { color: var(--muted); }
.breadcrumb a:hover { color: var(--primary); }
.breadcrumb .icon { font-size: 16px; }

/* --------------------------------------------------------------- Buttons */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    height: 38px;
    padding: 0 14px;
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    font: 600 14px/1 var(--font);
    cursor: pointer;
    white-space: nowrap;
    transition: background .12s, border-color .12s, box-shadow .12s, color .12s;
    background: var(--surface);
    color: var(--text);
    border-color: var(--border-strong);
    text-decoration: none;
}
.btn .icon { font-size: 19px; }
.btn:hover { background: var(--surface-2); color: var(--text); }
.btn:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--primary-ring); }
.btn:disabled, .btn[aria-disabled="true"] { opacity: .5; cursor: not-allowed; }

.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; box-shadow: var(--shadow-sm); }
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); color: #fff; }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { background: #c3343a; color: #fff; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-2); }
.btn-ghost:hover { background: var(--surface-3); }
.btn-sm { height: 32px; padding: 0 10px; font-size: 13px; }
.btn-lg { height: 44px; padding: 0 20px; font-size: 15px; }
.btn-block { width: 100%; }

.icon-btn {
    width: 34px; height: 34px;
    display: inline-grid; place-items: center;
    border-radius: var(--radius-sm);
    border: 0;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
    transition: background .12s, color .12s;
    padding: 0;
}
.icon-btn:hover { background: var(--surface-3); color: var(--text); }
.icon-btn.danger:hover { background: var(--danger-soft); color: var(--danger); }
.icon-btn.primary:hover { background: var(--primary-soft); color: var(--primary); }
.icon-btn:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--primary-ring); }
.icon-btn:disabled { opacity: .35; cursor: not-allowed; background: transparent; }

/* ----------------------------------------------------------------- Cards */

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
}
.card-body { padding: 20px; }
.card-header {
    display: flex; align-items: center; gap: 12px;
    padding: 14px 20px;
    border-bottom: 1px solid var(--border);
}
.card-header h2, .card-header h3 { font-size: 15px; }
.card-footer {
    display: flex; justify-content: flex-end; gap: 8px;
    padding: 14px 20px;
    border-top: 1px solid var(--border);
    background: var(--surface-2);
    border-radius: 0 0 var(--radius) var(--radius);
}

/* ---------------------------------------------------------------- Tables */

.table-toolbar {
    display: flex; align-items: center; gap: 10px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
}
.search-input { position: relative; flex: 1; max-width: 320px; }
.search-input .icon { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 18px; pointer-events: none; }
.search-input input { padding-left: 34px !important; height: 36px; }
.table-count { color: var(--muted); font-size: 13px; margin-left: auto; }

.table-wrap { overflow-x: auto; }
table.data {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
}
table.data th {
    text-align: left;
    font-size: 12px;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: .04em;
    padding: 10px 16px;
    background: var(--surface-2);
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}
table.data td {
    padding: 11px 16px;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}
table.data tbody tr:last-child td { border-bottom: 0; }
table.data tbody tr { transition: background .1s; }
table.data tbody tr:hover { background: var(--surface-2); }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data td.id { color: var(--muted); font-variant-numeric: tabular-nums; width: 60px; }
table.data td.actions { text-align: right; white-space: nowrap; width: 1%; }
table.data td.actions .row-actions { display: inline-flex; gap: 2px; }
table.data .primary-cell { font-weight: 600; color: var(--text); }
table.data .primary-cell:hover { color: var(--primary); }
table.data .secondary { color: var(--muted); font-size: 13px; max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
tr.clickable { cursor: pointer; }

.empty-state {
    text-align: center;
    padding: 48px 20px;
    color: var(--muted);
}
.empty-state .icon { font-size: 40px; color: var(--border-strong); margin-bottom: 8px; }
.empty-state h3 { font-size: 15px; color: var(--text); margin-bottom: 4px; }
.empty-state p { margin: 0 0 16px; }

/* ---------------------------------------------------------------- Badges */

.badge {
    display: inline-flex; align-items: center; gap: 4px;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
    background: var(--surface-3);
    color: var(--text-2);
    white-space: nowrap;
}
.badge.primary { background: var(--primary-soft); color: var(--primary); }
.badge.success { background: var(--success-soft); color: var(--success); }
.badge.warning { background: var(--warning-soft); color: var(--warning); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }

.dims { font-variant-numeric: tabular-nums; color: var(--text-2); white-space: nowrap; }
.dims .x { color: var(--muted); margin: 0 3px; }

.swatch {
    display: inline-block;
    width: 22px; height: 22px;
    border-radius: 6px;
    border: 1px solid rgba(0, 0, 0, .12);
    vertical-align: middle;
    box-shadow: inset 0 0 0 1px rgba(255,255,255,.25);
}

kbd {
    display: inline-block;
    min-width: 20px;
    padding: 1px 5px;
    font: 600 11px/1.5 var(--mono);
    text-align: center;
    color: var(--text-2);
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-bottom-width: 2px;
    border-radius: 5px;
}

/* ----------------------------------------------------------------- Forms */

.form-grid { display: grid; gap: 18px; grid-template-columns: repeat(12, 1fr); }
.form-grid > .col-12 { grid-column: span 12; }
.form-grid > .col-8 { grid-column: span 8; }
.form-grid > .col-6 { grid-column: span 6; }
.form-grid > .col-4 { grid-column: span 4; }
.form-grid > .col-3 { grid-column: span 3; }
@media (max-width: 640px) {
    .form-grid > .col-8, .form-grid > .col-6, .form-grid > .col-4, .form-grid > .col-3 { grid-column: span 12; }
}

.form-section-title {
    grid-column: span 12;
    font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em;
    color: var(--muted);
    padding-top: 6px;
}

.field label, label.field-label {
    display: block;
    font-weight: 600;
    font-size: 13px;
    color: var(--text-2);
    margin-bottom: 6px;
}
.field .hint { font-size: 12px; color: var(--muted); margin-top: 5px; }

.input, .field input[type="text"], .field input[type="number"], .field input[type="email"],
.field input[type="password"], .field select, .field textarea, .search-input input, select.input {
    width: 100%;
    height: 40px;
    padding: 0 12px;
    font: 400 14px var(--font);
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    transition: border-color .12s, box-shadow .12s;
    margin: 0;
    box-shadow: none;
}
.field textarea { height: auto; min-height: 96px; padding: 10px 12px; resize: vertical; line-height: 1.5; }
.field select, select.input { appearance: none; -webkit-appearance: none; padding-right: 34px; cursor: pointer;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5 6 6.5l5-5' fill='none' stroke='%237a8396' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat; background-position: right 12px center; }
.input:focus, .field input:focus, .field select:focus, .field textarea:focus, .search-input input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-ring);
}
.field input[readonly] { background: var(--surface-3); color: var(--text-2); cursor: default; }
.field input[readonly]:focus { box-shadow: none; border-color: var(--border-strong); }

.input-unit { position: relative; }
.input-unit input { padding-right: 46px !important; }
.input-unit .unit {
    position: absolute; right: 12px; top: 50%; transform: translateY(-50%);
    color: var(--muted); font-size: 13px; pointer-events: none;
}
input[type="number"]::-webkit-inner-spin-button { opacity: .4; }

.color-field { display: flex; align-items: center; gap: 10px; }
.color-field input[type="color"] {
    width: 48px; height: 40px; padding: 3px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--surface);
    cursor: pointer;
}
.color-field .color-hex { font-family: var(--mono); color: var(--text-2); font-size: 13px; }
.color-presets { display: flex; gap: 6px; flex-wrap: wrap; }
.color-presets button {
    width: 22px; height: 22px; border-radius: 6px; border: 1px solid rgba(0,0,0,.12); cursor: pointer; padding: 0;
}
.color-presets button:hover { transform: scale(1.12); }

.readonly-list { display: grid; gap: 2px; }
.kv { display: flex; justify-content: space-between; gap: 16px; padding: 10px 0; border-bottom: 1px dashed var(--border); }
.kv:last-child { border-bottom: 0; }
.kv .k { color: var(--muted); }
.kv .v { font-weight: 600; text-align: right; word-break: break-all; }
.kv .v.mono { font-family: var(--mono); font-weight: 500; font-size: 13px; }

/* ------------------------------------------------------------- Dashboard */

.stat-grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); }
.stat-card {
    display: flex; flex-direction: column; gap: 14px;
    padding: 18px;
    color: inherit;
    transition: border-color .15s, box-shadow .15s, transform .15s;
}
.stat-card:hover { border-color: var(--primary); box-shadow: var(--shadow); color: inherit; transform: translateY(-1px); }
.stat-card .stat-icon {
    width: 40px; height: 40px; border-radius: 10px;
    display: grid; place-items: center;
    background: var(--primary-soft); color: var(--primary);
}
.stat-card .stat-value { font-size: 28px; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; }
.stat-card .stat-label { color: var(--muted); font-weight: 500; }

.steps { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.step { display: flex; gap: 12px; align-items: flex-start; padding: 16px; }
.step-num {
    width: 28px; height: 28px; border-radius: 50%;
    background: var(--surface-3); color: var(--text-2);
    font-weight: 700; display: grid; place-items: center; flex-shrink: 0;
}
.step.done .step-num { background: var(--success-soft); color: var(--success); }
.step h4 { font-size: 14px; margin-bottom: 2px; }
.step p { margin: 0; color: var(--muted); font-size: 13px; }

/* ---------------------------------------------------------------- Toasts */

.toast-stack {
    position: fixed;
    right: 20px; bottom: 20px;
    display: flex; flex-direction: column; gap: 8px;
    z-index: 1000;
    pointer-events: none;
}
.toast {
    display: flex; align-items: center; gap: 10px;
    min-width: 260px; max-width: 420px;
    padding: 12px 14px;
    border-radius: var(--radius);
    background: #1f2533;
    color: #fff;
    box-shadow: var(--shadow-lg);
    font-weight: 500;
    pointer-events: auto;
    animation: toast-in .2s ease-out;
}
.toast .icon { font-size: 20px; }
.toast.success .icon { color: #4ade80; }
.toast.error .icon { color: #f87171; }
.toast.warning .icon { color: #fbbf24; }
.toast.info .icon { color: #93c5fd; }
.toast-action {
    margin-left: auto;
    padding: 5px 10px;
    border: 0; border-radius: 6px;
    background: rgba(255, 255, 255, .12);
    color: #c7cbff;
    font: inherit; font-weight: 600;
    cursor: pointer;
}
.toast-action:hover { background: rgba(255, 255, 255, .2); color: #fff; }
.toast.leaving { animation: toast-out .2s ease-in forwards; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px) scale(.98); } }
@keyframes toast-out { to { opacity: 0; transform: translateY(8px) scale(.98); } }

/* ---------------------------------------------------------------- Dialog */

dialog.modal {
    border: 0;
    padding: 0;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    width: min(440px, calc(100vw - 32px));
    color: var(--text);
}
dialog.modal::backdrop { background: rgba(15, 20, 30, .45); backdrop-filter: blur(2px); }
dialog.modal[open] { animation: dialog-in .16s ease-out; }
@keyframes dialog-in { from { opacity: 0; transform: translateY(6px) scale(.98); } }
.modal-body { padding: 22px 22px 8px; display: flex; gap: 14px; }
.modal-icon {
    width: 40px; height: 40px; border-radius: 50%;
    display: grid; place-items: center; flex-shrink: 0;
    background: var(--danger-soft); color: var(--danger);
}
.modal-body h3 { font-size: 16px; margin-bottom: 6px; }
.modal-body p { margin: 0; color: var(--text-2); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; padding: 18px 22px 20px; }

/* --------------------------------------------------------------- Helpers */

.muted { color: var(--muted); }
.mt-0 { margin-top: 0; }
.mb-16 { margin-bottom: 16px; }
.mb-24 { margin-bottom: 24px; }
.grow { flex: 1; }
.row { display: flex; gap: 10px; align-items: center; }
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); border: 0; }
.section-title { font-size: 15px; margin: 28px 0 12px; }

/* ------------------------------------- Label face (radio list + 3D view) */

.label-face-picker {
    display: grid;
    grid-template-columns: minmax(220px, 1fr) minmax(240px, 1.2fr);
    gap: 16px;
    align-items: stretch;
}
@media (max-width: 640px) { .label-face-picker { grid-template-columns: 1fr; } }

.face-list { display: flex; flex-direction: column; gap: 6px; }
.face-option {
    display: flex !important;
    align-items: center;
    gap: 10px;
    margin: 0 !important;
    padding: 8px 12px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--surface);
    cursor: pointer;
    font-weight: 400 !important;
    transition: border-color .12s, background .12s, box-shadow .12s;
}
.face-option:hover { border-color: var(--primary); }
.face-option input { position: absolute; opacity: 0; pointer-events: none; }
.face-option .radio-dot {
    width: 18px; height: 18px; flex-shrink: 0;
    border-radius: 50%;
    border: 2px solid var(--border-strong);
    background: var(--surface);
    transition: border-color .12s, box-shadow .12s;
}
.face-option .face-text { display: flex; flex-direction: column; line-height: 1.3; }
.face-option .face-text b { font-size: 13.5px; color: var(--text); font-weight: 600; }
.face-option .face-text small { font-size: 12px; color: var(--muted); }
.face-option:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); }
.face-option:has(input:checked) .radio-dot { border-color: var(--primary); box-shadow: inset 0 0 0 4px var(--surface); background: var(--primary); }
.face-option:has(input:focus-visible) { box-shadow: 0 0 0 3px var(--primary-ring); }

.face-preview {
    position: relative;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface-2);
    overflow: hidden;
    min-height: 300px;
    display: flex;
    flex-direction: column;
}
.face-preview-scene { position: relative; flex: 1; min-height: 260px; cursor: grab; }
.face-preview-scene canvas { position: absolute; inset: 0; }
.face-preview-caption {
    display: flex; align-items: center; gap: 6px;
    padding: 8px 12px;
    border-top: 1px solid var(--border);
    background: var(--surface);
    font-size: 12.5px; font-weight: 600; color: var(--text-2);
}
.face-preview-caption .icon { font-size: 17px; color: var(--muted); }
.face-preview-error { display: grid; place-items: center; height: 100%; color: var(--muted); font-size: 13px; padding: 20px; text-align: center; }
