:root {
    --bg: #f5f3ef;
    --surface: #ffffff;
    --surface-soft: #faf8f5;
    --text: #25221f;
    --muted: #746f69;
    --border: #e5e0d9;
    --brand: #9f2d24;
    --brand-dark: #752019;
    --success: #216e44;
    --danger: #a52722;
    --sidebar: #221d1a;
    --shadow: 0 14px 34px rgba(55, 45, 38, .08);
}

* { box-sizing: border-box; }
html { font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; color: var(--text); background: var(--bg); }
body { margin: 0; min-height: 100vh; }
a { color: var(--brand); text-decoration: none; }
button, input, select { font: inherit; }

.app-shell { display: grid; grid-template-columns: 260px minmax(0, 1fr); }
.sidebar { position: sticky; top: 0; height: 100vh; background: var(--sidebar); color: white; padding: 22px 18px; display: flex; flex-direction: column; z-index: 20; }
.brand, .login-brand { display: flex; align-items: center; gap: 12px; }
.brand { padding: 0 8px 24px; }
.brand-mark { display: inline-grid; place-items: center; width: 42px; height: 42px; border-radius: 12px; color: #fff; background: var(--brand); font-weight: 800; }
.brand strong, .brand small, .login-brand strong, .login-brand small { display: block; }
.brand small, .login-brand small { margin-top: 2px; color: #bdb5af; }
.sidebar nav { display: flex; flex-direction: column; gap: 4px; overflow-y: auto; }
.sidebar nav a { color: #eee8e3; padding: 10px 12px; border-radius: 8px; }
.sidebar nav a:hover { background: rgba(255,255,255,.09); }
.nav-heading { margin: 16px 12px 4px; color: #9d948d; font-size: .72rem; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; }
.logout-form { margin-top: auto; padding-top: 18px; }
.sidebar .button-ghost { width: 100%; color: white; border-color: #4b423d; }

.main-column { min-width: 0; }
.topbar { height: 66px; padding: 0 30px; border-bottom: 1px solid var(--border); background: rgba(255,255,255,.88); backdrop-filter: blur(12px); display: flex; align-items: center; gap: 14px; position: sticky; top: 0; z-index: 10; }
.menu-button { display: none; border: 0; background: transparent; font-size: 1.4rem; cursor: pointer; }
.content { max-width: 1400px; margin: 0 auto; padding: 30px; }
.page-heading { margin-bottom: 22px; }
.page-heading.split { display: flex; align-items: center; justify-content: space-between; gap: 20px; }
h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 8px; font-size: clamp(1.6rem, 3vw, 2.1rem); }
h2 { margin-bottom: 4px; font-size: 1.15rem; }
.page-heading p, .panel-heading p { color: var(--muted); margin-bottom: 0; }

.metric-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; margin-bottom: 24px; }
.dashboard-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 20px; margin-bottom: 20px; }
.dashboard-section { margin-bottom: 20px; }
.period-filter { display: flex; align-items: end; gap: 10px; }
.period-filter label { display: grid; gap: 5px; font-size: .8rem; font-weight: 700; }
.metric-card, .panel { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); }
.metric-card { padding: 20px; }
.metric-card span { color: var(--muted); font-size: .9rem; }
.metric-card strong { display: block; margin-top: 8px; font-size: 1.8rem; }
.panel { overflow: hidden; }
.panel-heading { padding: 20px 22px; border-bottom: 1px solid var(--border); }
.setup-list { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); }
.setup-list a { padding: 18px 22px; border-right: 1px solid var(--border); border-bottom: 1px solid var(--border); color: var(--text); }
.setup-list a:hover { background: var(--surface-soft); }
.setup-list strong, .setup-list span { display: block; }
.setup-list span { color: var(--muted); margin-top: 5px; font-size: .9rem; }

.button { display: inline-flex; align-items: center; justify-content: center; min-height: 42px; padding: 10px 16px; border-radius: 9px; border: 1px solid transparent; font-weight: 700; cursor: pointer; }
.button-primary { color: white; background: var(--brand); }
.button-primary:hover { background: var(--brand-dark); }
.button-ghost { color: var(--text); background: transparent; border-color: var(--border); }
.alert { margin: 0 0 18px; padding: 13px 15px; border-radius: 10px; border: 1px solid; }
.alert-success { color: var(--success); background: #edf8f1; border-color: #bfdfca; }
.alert-error { color: var(--danger); background: #fff1f0; border-color: #efc5c2; }
.alert ul { margin-bottom: 0; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; min-width: 780px; }
th, td { padding: 14px 16px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { color: var(--muted); background: var(--surface-soft); font-size: .78rem; letter-spacing: .04em; text-transform: uppercase; }
tbody tr:hover { background: #fcfbf9; }
.stock-alert, .stock-alert:hover { background: #fff1f0; color: var(--danger); }
.status { display: inline-flex; padding: 4px 8px; border-radius: 999px; background: #ece9e5; color: #625d57; font-size: .78rem; font-weight: 700; }
.status-active { background: #e7f5ec; color: var(--success); }
.empty-state { min-height: 220px; display: grid; place-content: center; text-align: center; color: var(--muted); }
.empty-state strong { color: var(--text); margin-bottom: 6px; }

.form-panel { padding: 22px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }
.form-grid label { display: grid; gap: 7px; font-weight: 700; font-size: .9rem; }
input, select { width: 100%; min-height: 44px; padding: 10px 12px; border: 1px solid #cbc4bc; border-radius: 9px; background: #fff; color: var(--text); }
input:focus, select:focus { outline: 3px solid rgba(159,45,36,.12); border-color: var(--brand); }
.checkbox-field { grid-template-columns: auto 1fr; align-items: center; align-self: end; min-height: 44px; }
.checkbox-field input { width: 20px; min-height: 20px; }
.form-actions { grid-column: 1 / -1; padding-top: 8px; }
.role-fieldset { grid-column: 1 / -1; border: 1px solid var(--border); border-radius: 10px; padding: 16px; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.role-fieldset legend { padding: 0 7px; font-weight: 800; }
details summary { color: var(--brand); cursor: pointer; }
pre { max-width: 420px; white-space: pre-wrap; word-break: break-word; font-size: .76rem; }

.auth-shell { display: grid; place-items: center; padding: 24px; background: radial-gradient(circle at top, #fff 0, var(--bg) 58%); }
.auth-content { width: min(100%, 440px); }
.login-card { background: var(--surface); padding: 34px; border: 1px solid var(--border); border-radius: 18px; box-shadow: var(--shadow); }
.login-brand { margin-bottom: 30px; }
.login-card h1 { margin-bottom: 6px; }
.login-card > p { color: var(--muted); margin-bottom: 24px; }
.form-stack { display: grid; gap: 16px; }
.form-stack label { display: grid; gap: 7px; font-weight: 700; font-size: .9rem; }
.error-page { min-height: 70vh; display: grid; place-content: center; justify-items: center; text-align: center; }
.error-page > strong { color: var(--brand); font-size: 5rem; }

@media (max-width: 900px) {
    .app-shell { display: block; }
    .sidebar { position: fixed; left: 0; transform: translateX(-105%); width: min(85vw, 280px); transition: transform .2s ease; box-shadow: 20px 0 50px rgba(0,0,0,.25); }
    .sidebar.is-open { transform: translateX(0); }
    .menu-button { display: inline-block; }
    .content { padding: 22px 18px; }
    .topbar { padding: 0 18px; }
    .metric-grid { grid-template-columns: repeat(2, minmax(0,1fr)); }
}

@media (max-width: 620px) {
    .page-heading.split { align-items: stretch; flex-direction: column; }
    .metric-grid, .setup-list, .form-grid, .role-fieldset, .dashboard-grid { grid-template-columns: 1fr; }
    .period-filter { align-items: stretch; flex-direction: column; }
    .setup-list a { border-right: 0; }
    .login-card { padding: 25px 20px; }
    .form-actions { grid-column: auto; }
}

.purchase-form { display: grid; gap: 20px; }
.purchase-section { display: grid; gap: 18px; }
.panel-heading-inline { display: flex; align-items: center; justify-content: space-between; gap: 18px; }
.panel-heading-inline h2, .panel-heading-inline p { margin-bottom: 0; }
.panel-heading-inline p { margin-top: 5px; color: var(--muted); }
.form-wide { grid-column: 1 / -1; }
.repeat-list { display: grid; gap: 14px; }
.repeat-row { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)) auto; gap: 14px; align-items: end; padding: 16px; border: 1px solid var(--border); border-radius: 11px; background: var(--surface-soft); }
.repeat-row label { display: grid; gap: 7px; font-weight: 700; font-size: .86rem; }
.repeat-remove { min-height: 44px; }
.purchase-summary { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); }
.purchase-summary div { padding: 18px 20px; border-right: 1px solid var(--border); }
.purchase-summary div:last-child { border-right: 0; }
.purchase-summary span, .purchase-summary strong { display: block; }
.purchase-summary span { color: var(--muted); font-size: .82rem; }
.purchase-summary strong { margin-top: 6px; font-size: 1.15rem; }
.summary-error { color: var(--danger); }
.purchase-submit { display: flex; justify-content: flex-end; align-items: center; gap: 18px; }
.purchase-submit p { margin: 0; color: var(--muted); }

@media (max-width: 1000px) {
    .repeat-row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .repeat-remove { align-self: end; }
}

@media (max-width: 620px) {
    .panel-heading-inline, .purchase-submit { align-items: stretch; flex-direction: column; }
    .repeat-row, .purchase-summary { grid-template-columns: 1fr; }
    .purchase-summary div { border-right: 0; border-bottom: 1px solid var(--border); }
    .purchase-summary div:last-child { border-bottom: 0; }
    .form-wide { grid-column: auto; }
}
