:root {
  --bg: #f7f3f7;
  --surface: #ffffff;
  --surface-soft: #fbf8fb;
  --text: #292229;
  --muted: #766d76;
  --primary: #8b5e83;
  --primary-dark: #68415f;
  --primary-soft: #eadde7;
  --border: #e5dce3;
  --danger: #a33b48;
  --danger-soft: #f9e2e5;
  --warning: #9a681a;
  --warning-soft: #fff2d9;
  --success: #347458;
  --success-soft: #dff1e8;
  --shadow: 0 14px 40px rgba(58, 35, 54, 0.08);
  --radius: 18px;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--text);
  background: var(--bg);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.58; }

body.auth-pending,
body.auth-locked { overflow: hidden; }

body.auth-pending .topbar,
body.auth-pending .app-shell,
body.auth-pending .mobile-nav,
body.auth-locked .topbar,
body.auth-locked .app-shell,
body.auth-locked .mobile-nav { display: none; }

.login-screen {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: grid;
  place-items: center;
  min-height: 100dvh;
  padding: 24px;
  overflow-y: auto;
  background:
    radial-gradient(circle at 20% 15%, rgba(183, 130, 170, 0.25), transparent 34%),
    radial-gradient(circle at 85% 80%, rgba(139, 94, 131, 0.16), transparent 32%),
    var(--bg);
}

.login-card {
  width: min(440px, 100%);
  padding: 34px;
  text-align: center;
  background: rgba(255, 255, 255, 0.96);
  border: 1px solid var(--border);
  border-radius: 24px;
  box-shadow: 0 24px 70px rgba(58, 35, 54, 0.14);
}

.login-brand {
  display: grid;
  place-items: center;
  width: 66px;
  height: 66px;
  margin: 0 auto 20px;
  color: #fff;
  background: linear-gradient(145deg, var(--primary), #b782aa);
  border-radius: 21px;
  font-family: Georgia, serif;
  font-size: 35px;
  font-style: italic;
  box-shadow: 0 12px 26px rgba(139, 94, 131, 0.28);
}

.login-card h1 { margin: 0; font-size: clamp(27px, 7vw, 35px); }
.login-card > .muted { margin: 10px 0 0; }
.login-form { display: grid; gap: 15px; margin-top: 24px; text-align: left; }
.login-form .primary-button { width: 100%; margin-top: 4px; }
.login-message { margin: 18px 0 0; padding: 12px; color: var(--danger); background: var(--danger-soft); border-radius: 10px; font-size: 13px; }
.login-help { margin: 22px 0 0; color: var(--muted); font-size: 12px; line-height: 1.55; }
.text-link { display: inline-block; margin-top: 12px; color: var(--primary-dark); font-size: 13px; font-weight: 750; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 70px;
  padding: 12px 22px;
  background: rgba(255, 255, 255, 0.92);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(14px);
}

.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  color: white;
  background: linear-gradient(145deg, var(--primary), #b782aa);
  border-radius: 14px;
  font-family: Georgia, serif;
  font-size: 23px;
  font-style: italic;
  box-shadow: 0 8px 18px rgba(139, 94, 131, 0.26);
}
.brand strong, .brand small { display: block; }
.brand small { margin-top: 2px; color: var(--muted); font-size: 12px; }
.brand small[data-state="saving"],
.brand small[data-state="pending"] { color: var(--warning); }
.brand small[data-state="synced"] { color: var(--success); }
.brand small[data-state="error"] { color: var(--danger); }
.topbar-actions { display: flex; align-items: center; gap: 8px; }

.app-shell { display: grid; grid-template-columns: 220px minmax(0, 1fr); }
.sidebar {
  position: sticky;
  top: 70px;
  align-self: start;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: calc(100vh - 70px);
  padding: 24px 16px;
  border-right: 1px solid var(--border);
  background: var(--surface-soft);
}

.nav-button, .mobile-nav-button {
  border: 0;
  color: var(--muted);
  background: transparent;
}
.nav-button {
  padding: 13px 14px;
  border-radius: 12px;
  text-align: left;
  font-weight: 650;
}
.nav-button:hover, .nav-button.active {
  color: var(--primary-dark);
  background: var(--primary-soft);
}

.content { min-width: 0; padding: 34px; }
.page { display: none; }
.page.active { display: block; animation: pageIn 180ms ease-out; }
@keyframes pageIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.page-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 24px;
}
.page-heading h1 { margin: 0; font-size: clamp(27px, 4vw, 39px); line-height: 1.08; }
.page-heading .muted { margin: 8px 0 0; }
.heading-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 10px; }
.eyebrow {
  margin: 0 0 7px;
  color: var(--primary);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}
.muted { color: var(--muted); }

button, .file-button {
  border-radius: 11px;
  transition: transform 120ms ease, box-shadow 120ms ease, background 120ms ease;
}
button:active, .file-button:active { transform: translateY(1px); }
.primary-button, .secondary-button, .ghost-button, .danger-button, .file-button {
  min-height: 42px;
  padding: 10px 15px;
  border: 1px solid transparent;
  font-weight: 750;
}
.primary-button { color: #fff; background: var(--primary); box-shadow: 0 8px 18px rgba(139, 94, 131, 0.18); }
.primary-button:hover { background: var(--primary-dark); }
.secondary-button { color: var(--primary-dark); background: var(--primary-soft); border-color: #dac7d5; }
.ghost-button { color: var(--text); background: #fff; border-color: var(--border); }
.danger-button { color: var(--danger); background: var(--danger-soft); border-color: #efc9ce; }
.compact { min-height: 38px; padding: 8px 12px; }
.icon-button { width: 38px; height: 38px; padding: 0; border: 0; color: var(--muted); background: transparent; font-size: 28px; }

.metric-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; margin-bottom: 20px; }
.metric-card {
  padding: 21px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.metric-card span, .metric-card small { display: block; color: var(--muted); }
.metric-card strong { display: block; margin: 8px 0 2px; font-size: 34px; }
.metric-card.attention { background: linear-gradient(145deg, #fff, #f7edf4); }
.metric-card.warning { background: linear-gradient(145deg, #fff, #fff7e9); }

.dashboard-grid { display: grid; grid-template-columns: 1.15fr 0.85fr; gap: 20px; margin-bottom: 20px; }
.panel {
  padding: 22px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.panel-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 17px; }
.panel-heading h2, .section-heading h3 { margin: 0; }
.panel-heading p, .section-heading p { margin: 5px 0 0; color: var(--muted); font-size: 14px; }

.stack-list { display: flex; flex-direction: column; gap: 11px; }
.stack-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 13px;
  background: var(--surface-soft);
}
.stack-item-main { min-width: 0; }
.stack-item-main strong { display: block; }
.stack-item-main span { display: block; margin-top: 4px; color: var(--muted); font-size: 13px; }
.stack-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 7px; }
.compact-list .stack-item { padding: 10px 12px; }
.empty-state { padding: 30px 16px; color: var(--muted); text-align: center; border: 1px dashed var(--border); border-radius: 13px; }

.table-wrap { width: 100%; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 13px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { color: var(--muted); font-size: 12px; letter-spacing: 0.04em; text-transform: uppercase; }
tbody tr:hover { background: #fcfafc; }
.table-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.small-button { min-height: 34px; padding: 6px 9px; border: 1px solid var(--border); color: var(--text); background: #fff; font-size: 12px; font-weight: 700; }
.small-button.success { color: var(--success); background: var(--success-soft); border-color: #bfdcca; }
.small-button.danger { color: var(--danger); background: var(--danger-soft); border-color: #efc9ce; }

.badge { display: inline-flex; align-items: center; min-height: 26px; padding: 4px 9px; border-radius: 999px; font-size: 12px; font-weight: 800; white-space: nowrap; }
.badge.neutral { color: var(--muted); background: #eee9ed; }
.badge.scheduled { color: #725111; background: #fff0c9; }
.badge.completed { color: var(--success); background: var(--success-soft); }
.badge.canceled { color: var(--danger); background: var(--danger-soft); }
.badge.low { color: var(--danger); background: var(--danger-soft); }

.filter-bar {
  display: flex;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 16px;
  padding: 15px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
}
.filter-bar label { min-width: 180px; }
.filter-bar.one-line .grow { flex: 1; }

label { display: flex; flex-direction: column; gap: 7px; color: #4d454c; font-size: 13px; font-weight: 700; }
input, select, textarea {
  width: 100%;
  min-height: 43px;
  padding: 10px 12px;
  border: 1px solid #d8ced6;
  border-radius: 10px;
  color: var(--text);
  background: #fff;
  outline: none;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(139, 94, 131, 0.12); }
label small { color: var(--muted); font-weight: 500; }
.checkbox-label { flex-direction: row; align-items: center; align-self: end; min-height: 43px; }
.checkbox-label input { width: 18px; min-height: auto; }

.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 15px; }
.full-width { grid-column: 1 / -1; }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }
.form-actions.wrap { flex-wrap: wrap; justify-content: flex-start; }

.card-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 15px; }
.service-card { padding: 18px; border: 1px solid var(--border); border-radius: 15px; background: var(--surface-soft); }
.service-card h3 { margin: 0; }
.service-card-price { margin: 9px 0 13px; color: var(--primary-dark); font-size: 24px; font-weight: 850; }
.service-meta { display: grid; gap: 7px; margin-bottom: 14px; color: var(--muted); font-size: 13px; }
.recipe-summary { margin-top: 12px; padding-top: 12px; border-top: 1px dashed var(--border); color: var(--muted); font-size: 13px; }
.service-actions { margin-top: 15px; }

.settings-grid { display: grid; grid-template-columns: 1fr 0.75fr; gap: 20px; }
.cloud-panel { grid-column: 1 / -1; }
.route-form-panel { max-width: 780px; }
.security-notice { border-left: 4px solid var(--success); }
.current-user-row { background: var(--surface-soft); }
.user-identity strong,
.user-identity small { display: block; }
.user-identity small { margin-top: 4px; color: var(--muted); }
.button-stack { display: flex; flex-direction: column; gap: 10px; }
.file-button { display: flex; align-items: center; justify-content: center; color: var(--primary-dark); background: var(--primary-soft); border-color: #dac7d5; cursor: pointer; }
.file-button input { display: none; }
.notice { margin: 16px 0 0; padding: 12px; color: var(--muted); background: var(--surface-soft); border-radius: 10px; font-size: 13px; }
.notice.message-success { color: var(--success); background: var(--success-soft); }
.notice.message-error { color: var(--danger); background: var(--danger-soft); }

.report-section + .report-section { margin-top: 36px; padding-top: 34px; border-top: 1px solid var(--border); }
.report-heading { margin-bottom: 17px; }
.report-heading h2 { margin: 0; font-size: 24px; }
.report-filters .grow { flex: 1; }
.report-metrics { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.report-metrics .metric-card strong { font-size: clamp(24px, 3vw, 34px); }
.stock-report-metrics { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.report-panel { box-shadow: none; }
.stock-balance { display: block; margin-top: 4px; color: var(--muted); font-size: 12px; }

.modal {
  width: min(620px, calc(100% - 24px));
  max-height: calc(100vh - 30px);
  padding: 0;
  border: 0;
  border-radius: 18px;
  box-shadow: 0 24px 70px rgba(35, 21, 33, 0.28);
}
.modal.wide { width: min(820px, calc(100% - 24px)); }
.modal::backdrop { background: rgba(28, 19, 27, 0.5); backdrop-filter: blur(3px); }
.modal-card { padding: 22px; overflow-y: auto; }
.modal-heading { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; }
.modal-heading h2 { margin: 0; }
.recipe-section { margin-top: 22px; padding: 17px; border: 1px solid var(--border); border-radius: 14px; background: var(--surface-soft); }
.recipe-builder { display: grid; grid-template-columns: minmax(0, 1fr) 150px auto; gap: 10px; margin: 15px 0; }
.stock-preview { margin-top: 16px; }
.stock-preview:not(:empty) { padding: 12px; color: var(--muted); background: var(--surface-soft); border: 1px solid var(--border); border-radius: 11px; font-size: 13px; }

.toast-container { position: fixed; right: 18px; bottom: 18px; z-index: 100; display: flex; flex-direction: column; gap: 9px; }
.toast { min-width: 260px; max-width: 380px; padding: 13px 15px; color: #fff; background: #3e343c; border-radius: 11px; box-shadow: 0 12px 30px rgba(0,0,0,.18); animation: toastIn 180ms ease-out; }
.toast.error { background: var(--danger); }
.toast.success { background: var(--success); }
@keyframes toastIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.mobile-nav { display: none; }

@media (max-width: 1080px) {
  .metric-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .dashboard-grid, .settings-grid { grid-template-columns: 1fr; }
  .cloud-panel { grid-column: auto; }
  .card-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .report-metrics { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .stock-report-metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 760px) {
  body { padding-bottom: 72px; }
  .topbar { min-height: 62px; padding: 10px 14px; }
  .brand-mark { width: 38px; height: 38px; }
  .app-shell { display: block; }
  .sidebar { display: none; }
  .content { padding: 20px 13px; }
  .page-heading { align-items: stretch; flex-direction: column; margin-bottom: 18px; }
  .page-heading > button { width: 100%; }
  .heading-actions { flex-direction: column-reverse; }
  .heading-actions button { width: 100%; }
  .metric-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .metric-card { padding: 16px; }
  .metric-card strong { font-size: 29px; }
  .panel { padding: 16px; }
  .dashboard-grid, .card-grid, .form-grid { grid-template-columns: 1fr; }
  .full-width { grid-column: auto; }
  .stack-item { align-items: flex-start; flex-direction: column; }
  .stack-actions { justify-content: flex-start; }
  .filter-bar { align-items: stretch; flex-direction: column; }
  .filter-bar label { min-width: 0; }
  .recipe-builder { grid-template-columns: 1fr; }
  .form-actions { flex-direction: column-reverse; }
  .form-actions button { width: 100%; }
  .mobile-nav {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 30;
    display: grid;
    grid-template-columns: none;
    grid-auto-flow: column;
    grid-auto-columns: minmax(50px, 1fr);
    min-height: 64px;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    padding-bottom: env(safe-area-inset-bottom);
    background: rgba(255, 255, 255, 0.96);
    border-top: 1px solid var(--border);
    backdrop-filter: blur(14px);
  }
  .mobile-nav-button { padding: 8px 4px; font-size: 11px; font-weight: 750; }
  .mobile-nav-button.active { color: var(--primary-dark); background: var(--primary-soft); }
  .toast-container { right: 10px; bottom: 80px; left: 10px; }
  .toast { min-width: 0; max-width: none; }
}

@media (max-width: 410px) {
  .metric-grid { grid-template-columns: 1fr; }
  .report-metrics, .stock-report-metrics { grid-template-columns: 1fr; }
  .topbar .compact { padding-inline: 10px; font-size: 12px; }
  .topbar .ghost-button { display: none; }
  .mobile-nav-button { font-size: 10px; }
}

@media print {
  body { padding: 0; color: #000; background: #fff; }
  .topbar, .sidebar, .mobile-nav, .page:not(#page-reports), #printReportsButton, .report-filters, .toast-container { display: none !important; }
  .app-shell { display: block; }
  .content { padding: 0; }
  #page-reports { display: block !important; }
  .page-heading { margin-bottom: 20px; }
  .panel, .metric-card { box-shadow: none; break-inside: avoid; }
  .report-section { break-inside: auto; }
  .report-section + .report-section { break-before: page; }
  .table-wrap { overflow: visible; }
  table { font-size: 11px; }
}
