:root {
    --bg: #f5f3ee;
    --surface: #fffdf8;
    --surface-2: #ece7dc;
    --text: #1f2421;
    --muted: #69716b;
    --line: #d8d0c2;
    --primary: #116149;
    --primary-strong: #0b4333;
    --accent: #d97706;
    --danger: #b42318;
    --safe: #12805c;
    --shadow: 0 18px 45px rgba(30, 34, 28, .09);
}

* { box-sizing: border-box; }
html { min-height: 100%; }
body {
    min-height: 100%;
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    letter-spacing: 0;
}
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; }
input, select, textarea {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: #fffefa;
    color: var(--text);
    padding: 11px 12px;
    outline: none;
}
textarea { resize: vertical; }
label {
    display: grid;
    gap: 7px;
    color: var(--muted);
    font-size: 13px;
    font-weight: 700;
}
input:focus, select:focus, textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(17, 97, 73, .14);
}

.app-shell { display: grid; grid-template-columns: 268px minmax(0, 1fr); min-height: 100vh; }
.sidebar {
    position: sticky;
    top: 0;
    height: 100vh;
    padding: 22px;
    background: #1f2421;
    color: #f9f5eb;
    display: flex;
    flex-direction: column;
    gap: 24px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand small { display: block; color: #c9c0ad; margin-top: 2px; }
.brand-mark {
    width: 42px;
    height: 42px;
    display: inline-grid;
    place-items: center;
    border-radius: 3px;
    background: #eab308;
    color: #1f2421;
    font-weight: 900;
}
.brand-mark.large { width: 58px; height: 58px; font-size: 22px; }
.nav { display: grid; gap: 6px; }
.nav a {
    padding: 11px 12px;
    border-radius: 6px;
    color: #d9d1bf;
    font-weight: 700;
}
.nav a:hover, .nav a.active { background: #f9f5eb; color: #1f2421; }
.logout-form { margin-top: auto; }
.main { padding: 28px; min-width: 0; }
.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    margin-bottom: 20px;
}
.topbar h1, h2, h1 { margin: 0; line-height: 1.15; }
.eyebrow {
    margin: 0 0 6px;
    color: var(--primary);
    font-size: 12px;
    font-weight: 900;
    text-transform: uppercase;
    letter-spacing: .08em;
}
.user-chip {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 10px 14px;
    color: var(--muted);
    font-weight: 800;
}

.panel, .metric, .login-card {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: var(--shadow);
}
.panel { padding: 20px; margin-bottom: 20px; }
.panel-heading {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 16px;
    margin-bottom: 18px;
}
.dashboard-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 16px;
    margin-bottom: 20px;
}
.metric { padding: 20px; border-left: 5px solid var(--primary); }
.metric.warning { border-left-color: var(--accent); }
.metric.danger { border-left-color: var(--danger); }
.metric span { display: block; color: var(--muted); font-size: 13px; font-weight: 800; }
.metric strong { display: block; margin-top: 12px; font-size: clamp(28px, 4vw, 42px); line-height: 1; }
.panel-grid { display: grid; grid-template-columns: minmax(0, 1.35fr) minmax(300px, .65fr); gap: 20px; }

.button {
    min-height: 42px;
    border: 0;
    border-radius: 6px;
    padding: 10px 15px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    font-weight: 900;
    cursor: pointer;
    transition: transform .16s ease, box-shadow .16s ease, background .16s ease;
}
.button:hover { transform: translateY(-1px); }
.button.primary { background: var(--primary); color: #fff; box-shadow: 0 10px 20px rgba(17, 97, 73, .18); }
.button.primary:hover { background: var(--primary-strong); }
.button.secondary { background: var(--surface-2); color: var(--text); }
.button.ghost { background: transparent; color: inherit; border: 1px solid currentColor; }
.button.danger { background: #fee4df; color: var(--danger); }
.button.small { min-height: 34px; padding: 7px 10px; font-size: 12px; }
.button.wide { width: 100%; }
.actions { display: flex; gap: 8px; align-items: center; }
.actions form { margin: 0; }

.alert {
    border-radius: 6px;
    padding: 12px 14px;
    margin-bottom: 16px;
    font-weight: 800;
}
.alert.success { background: #dff6eb; color: #0d6146; }
.alert.error { background: #fee4df; color: var(--danger); }
.floating-alert { position: fixed; top: 18px; right: 18px; z-index: 5; }

.table-wrap { width: 100%; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; min-width: 760px; }
th, td { padding: 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { color: var(--muted); font-size: 12px; text-transform: uppercase; }
td small { display: block; color: var(--muted); margin-top: 4px; }
.empty-state { color: var(--muted); text-align: center; padding: 28px; }
.badge {
    display: inline-flex;
    align-items: center;
    border-radius: 999px;
    padding: 5px 10px;
    font-size: 12px;
    font-weight: 900;
}
.badge.safe, .progress .safe { background: #dff6eb; color: var(--safe); }
.badge.low, .progress .low { background: #fff3d6; color: #a15c00; }
.badge.empty, .progress .empty { background: #fee4df; color: var(--danger); }

.filter-bar {
    display: grid;
    grid-template-columns: repeat(4, minmax(160px, 1fr));
    gap: 12px;
    align-items: end;
    margin-bottom: 18px;
}
.form-shell { max-width: 920px; }
.form-card { display: grid; gap: 16px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.form-actions, .report-actions, .settings-line { display: flex; gap: 10px; align-items: center; justify-content: flex-end; }
.checkbox { display: flex; grid-auto-flow: column; align-items: center; justify-content: start; gap: 9px; }
.checkbox input { width: auto; }

.stock-bar { display: grid; gap: 8px; margin-bottom: 18px; }
.stock-bar > div:first-child { display: flex; justify-content: space-between; font-weight: 900; }
.progress { height: 12px; border-radius: 999px; overflow: hidden; background: var(--surface-2); }
.progress span { display: block; height: 100%; transition: width .4s ease; }
.stock-hint {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    border: 1px solid #b7d8ca;
    border-radius: 6px;
    background: #e9f7f0;
    color: #0d6146;
    padding: 14px 16px;
}
.stock-hint span {
    color: #3d725f;
    font-size: 13px;
    font-weight: 900;
    text-transform: uppercase;
}
.stock-hint strong { font-size: 22px; }

.auth-page {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
    background:
        linear-gradient(135deg, rgba(17, 97, 73, .1), transparent 45%),
        var(--bg);
}
.login-panel {
    width: min(980px, 100%);
    display: grid;
    grid-template-columns: 1fr 420px;
    gap: 22px;
    align-items: stretch;
}
.login-copy {
    padding: clamp(28px, 5vw, 58px);
    border: 2px solid #1f2421;
    border-radius: 8px;
    background: #e7ddc5;
}
.login-copy h1 { font-size: clamp(34px, 6vw, 68px); max-width: 9ch; }
.login-copy p:last-child { max-width: 52ch; color: #4f584f; font-weight: 700; line-height: 1.6; }
.login-card { padding: 28px; display: grid; gap: 16px; align-content: center; }
.muted { color: var(--muted); font-size: 13px; }
.print-title { display: none; }
.nested-panel { box-shadow: none; background: #faf6ec; }
.tool-pills { display: flex; flex-wrap: wrap; gap: 8px; }
.tool-pills span {
    border: 1px solid var(--line);
    border-radius: 999px;
    background: #fffefa;
    color: var(--muted);
    padding: 6px 10px;
    font-size: 12px;
    font-weight: 900;
}
.ai-widget { position: fixed; right: 22px; bottom: 22px; z-index: 20; }
.ai-toggle {
    width: 58px;
    height: 58px;
    border: 0;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    box-shadow: 0 18px 34px rgba(17, 97, 73, .28);
    font-weight: 950;
    cursor: pointer;
}
.ai-drawer {
    position: fixed;
    right: 22px;
    bottom: 92px;
    width: min(430px, calc(100vw - 28px));
    max-height: min(680px, calc(100vh - 120px));
    display: none;
    grid-template-rows: auto minmax(220px, 1fr) auto;
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface);
    box-shadow: 0 24px 70px rgba(31, 36, 33, .22);
}
.ai-drawer.open { display: grid; }
.ai-drawer header {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    align-items: center;
    padding: 14px 16px;
    background: #1f2421;
    color: #f9f5eb;
}
.ai-drawer header small { display: block; color: #d9d1bf; margin-top: 3px; }
.ai-header-actions { display: flex; align-items: center; gap: 8px; }
.ai-clear {
    height: 34px;
    border: 1px solid #d9d1bf;
    border-radius: 999px;
    background: transparent;
    color: #f9f5eb;
    padding: 0 12px;
    font-size: 12px;
    font-weight: 900;
    cursor: pointer;
}
.ai-close {
    width: 34px;
    height: 34px;
    border: 1px solid #d9d1bf;
    border-radius: 50%;
    background: transparent;
    color: #f9f5eb;
    cursor: pointer;
}
.ai-messages { padding: 14px; overflow-y: auto; display: grid; gap: 10px; align-content: start; }
.ai-message {
    border-radius: 8px;
    padding: 10px 12px;
    font-size: 13px;
    line-height: 1.45;
}
.ai-message.assistant { background: #edf7f2; color: #0d6146; }
.ai-message.user { background: #f4ead6; color: #3c3322; justify-self: end; }
.ai-message.warning { background: #fff3d6; color: #8a4f00; }
.ai-message a { text-decoration: underline; }
.ai-thinking { display: inline-flex; align-items: center; gap: 7px; justify-self: start; }
.ai-thinking span { display: inline-flex; gap: 4px; align-items: center; }
.ai-thinking i {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #0d6146;
    opacity: .45;
    animation: aiThinkingBounce .9s infinite ease-in-out;
}
.ai-thinking i:nth-child(2) { animation-delay: .15s; }
.ai-thinking i:nth-child(3) { animation-delay: .3s; }
.ai-form {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 10px;
    align-items: end;
    padding: 12px 14px 14px;
    border-top: 1px solid var(--line);
    background: #fbf7ed;
}
.ai-form textarea {
    min-height: 58px;
    max-height: 130px;
    border-radius: 8px;
    padding: 12px;
    line-height: 1.45;
}
.ai-form .button.primary {
    width: 74px;
    min-height: 46px;
    height: 46px;
    align-self: end;
    border-radius: 8px;
    padding: 0 14px;
    box-shadow: none;
}
.ai-form button[disabled], .ai-form textarea[disabled], .ai-preview button[disabled] { opacity: .62; cursor: not-allowed; }
.ai-preview {
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fffefa;
    overflow: hidden;
}
.ai-preview-body { display: grid; gap: 8px; padding: 12px; color: #344039; font-size: 13px; }
.ai-preview-body strong { color: var(--primary); }
.ai-preview-body p { margin: 0; white-space: pre-line; line-height: 1.5; }
.ai-preview-actions { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--line); }

@keyframes aiThinkingBounce {
    0%, 80%, 100% { transform: translateY(0); opacity: .45; }
    40% { transform: translateY(-4px); opacity: 1; }
}

@media (max-width: 980px) {
    .app-shell { grid-template-columns: 1fr; }
    .sidebar {
        position: relative;
        height: auto;
        padding: 14px;
        gap: 12px;
    }
    .nav { grid-template-columns: repeat(3, 1fr); }
    .logout-form { margin-top: 0; }
    .main { padding: 18px; }
    .dashboard-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .panel-grid, .login-panel { grid-template-columns: 1fr; }
    .filter-bar { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 640px) {
    .topbar, .panel-heading, .form-actions, .report-actions { flex-direction: column; align-items: stretch; }
    .dashboard-grid, .form-grid, .filter-bar, .nav { grid-template-columns: 1fr; }
    .brand { align-items: flex-start; }
    .main { padding: 14px; }
    .panel { padding: 15px; }
    .login-copy h1 { max-width: none; }
    .ai-widget { right: 14px; bottom: 14px; }
    .ai-drawer { right: 14px; bottom: 82px; }
    .ai-form { grid-template-columns: minmax(0, 1fr) auto; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { transition: none !important; scroll-behavior: auto !important; }
    .ai-thinking i { animation: none !important; opacity: .7; }
}

@media print {
    body { background: #fff; color: #000; font-size: 12px; }
    .app-shell { display: block; }
    .main { padding: 0; }
    .panel { box-shadow: none; border: 0; padding: 0; }
    .no-print, .sidebar, .topbar, .alert { display: none !important; }
    .print-title { display: block; margin-bottom: 16px; }
    table { min-width: 0; font-size: 11px; }
    th, td { padding: 7px; border: 1px solid #bbb; }
}
