@import url('https://fonts.googleapis.com/css2?family=Fraunces:opsz,wght@9..144,500;9..144,600;9..144,700&family=Inter:wght@400;500;600;700&display=swap');

:root {
    --ink: #1C2321;
    --ink-soft: #4B5754;
    --paper: #F6F5F1;
    --surface: #FFFFFF;
    --border: #E1DFD6;
    --primary: #1F6F5C;
    --primary-dark: #164F41;
    --primary-tint: #E4EFEC;
    --accent: #B65C2E;
    --accent-tint: #F6E7DD;
    --danger: #A8352B;
    --danger-tint: #F6E0DD;
    --radius: 10px;
    --shadow: 0 1px 2px rgba(28, 35, 33, 0.06);
    font-size: 16px;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--paper);
    color: var(--ink);
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    -webkit-font-smoothing: antialiased;
}

h1, h2, h3, .display {
    font-family: 'Fraunces', Georgia, serif;
    font-weight: 600;
    letter-spacing: -0.01em;
    margin: 0 0 0.4em;
    color: var(--ink);
}

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

p { line-height: 1.55; }

:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

/* ---------- Auth pages ---------- */
.auth-shell {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
}

.auth-card {
    width: 100%;
    max-width: 380px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 36px 32px;
    box-shadow: var(--shadow);
}

.auth-card .brand {
    text-align: center;
    margin-bottom: 28px;
}

.auth-card .brand .mark {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 48px;
    height: 48px;
    border-radius: 12px;
    background: var(--primary);
    color: #fff;
    font-family: 'Fraunces', serif;
    font-size: 22px;
    font-weight: 600;
    margin-bottom: 12px;
}

.auth-card h1 { font-size: 1.4rem; margin-bottom: 2px; }
.auth-card .sub { color: var(--ink-soft); font-size: 0.92rem; }

.auth-foot {
    text-align: center;
    margin-top: 18px;
    font-size: 0.9rem;
    color: var(--ink-soft);
}

/* ---------- Forms ---------- */
.field { margin-bottom: 16px; }
.field label {
    display: block;
    font-size: 0.84rem;
    font-weight: 600;
    margin-bottom: 6px;
    color: var(--ink);
}
.field .hint { font-size: 0.78rem; color: var(--ink-soft); margin-top: 4px; }

input[type="text"],
input[type="password"],
input[type="email"],
input[type="number"],
input[type="file"],
select,
textarea {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.95rem;
    font-family: inherit;
    color: var(--ink);
    background: #fff;
}
textarea { resize: vertical; min-height: 80px; }
input:focus, select:focus, textarea:focus { border-color: var(--primary); }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 18px;
    border-radius: 8px;
    border: 1px solid transparent;
    background: var(--primary);
    color: #fff;
    font-weight: 600;
    font-size: 0.92rem;
    cursor: pointer;
    font-family: inherit;
}
.btn:hover { background: var(--primary-dark); text-decoration: none; }
.btn.block { width: 100%; }
.btn.secondary { background: #fff; color: var(--ink); border-color: var(--border); }
.btn.secondary:hover { background: var(--paper); }
.btn.danger { background: var(--danger); }
.btn.danger:hover { background: #832920; }
.btn.small { padding: 6px 12px; font-size: 0.82rem; }

.alert {
    padding: 12px 14px;
    border-radius: 8px;
    font-size: 0.88rem;
    margin-bottom: 18px;
    border: 1px solid transparent;
}
.alert.success { background: var(--primary-tint); color: var(--primary-dark); border-color: #cfe4dd; }
.alert.error   { background: var(--danger-tint); color: var(--danger); border-color: #edcac5; }

/* ---------- App shell ---------- */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
    width: 232px;
    flex-shrink: 0;
    background: var(--ink);
    color: #EDEDE9;
    padding: 22px 16px;
    display: flex;
    flex-direction: column;
}

.sidebar .brand {
    font-family: 'Fraunces', serif;
    font-size: 1.15rem;
    font-weight: 600;
    color: #fff;
    padding: 0 8px 20px;
    border-bottom: 1px solid rgba(255,255,255,0.12);
    margin-bottom: 16px;
}
.sidebar .brand .role-tag {
    display: block;
    font-family: 'Inter', sans-serif;
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--accent);
    margin-top: 4px;
}

.nav-link {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 10px;
    border-radius: 8px;
    color: #CFCFC8;
    font-size: 0.9rem;
    font-weight: 500;
    margin-bottom: 2px;
}
.nav-link:hover { background: rgba(255,255,255,0.08); text-decoration: none; color: #fff; }
.nav-link.active { background: var(--primary); color: #fff; }
.nav-link .badge-count {
    margin-left: auto;
    background: var(--accent);
    color: #fff;
    font-size: 0.7rem;
    font-weight: 700;
    padding: 1px 7px;
    border-radius: 999px;
}

.sidebar .spacer { flex: 1; }
.sidebar .user-block {
    border-top: 1px solid rgba(255,255,255,0.12);
    padding-top: 14px;
    font-size: 0.85rem;
}
.sidebar .user-block .name { color: #fff; font-weight: 600; }
.sidebar .user-block .role { color: #9A9A92; font-size: 0.78rem; text-transform: capitalize; }
.sidebar .user-block a { color: #CFCFC8; font-size: 0.8rem; }

.main {
    flex: 1;
    min-width: 0;
    padding: 28px 34px 80px;
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 26px;
    gap: 16px;
}
.topbar h1 { font-size: 1.5rem; margin: 0; }
.topbar .sub { color: var(--ink-soft); font-size: 0.9rem; margin-top: 2px; }

.mobile-toggle { display: none; }

/* ---------- Stat cards ---------- */
.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
    gap: 14px;
    margin-bottom: 30px;
}
.stat-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 18px 20px;
}
.stat-card .label { font-size: 0.78rem; color: var(--ink-soft); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.stat-card .value { font-family: 'Fraunces', serif; font-size: 1.85rem; font-weight: 600; margin-top: 6px; }
.stat-card.alert .value { color: var(--accent); }

/* ---------- Panels & tables ---------- */
.panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 24px;
    overflow: hidden;
}
.panel-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    gap: 12px;
    flex-wrap: wrap;
}
.panel-head h2 { font-size: 1.05rem; margin: 0; }
.panel-body { padding: 20px; }

table { width: 100%; border-collapse: collapse; font-size: 0.89rem; }
th, td { text-align: left; padding: 11px 20px; border-bottom: 1px solid var(--border); }
th { font-size: 0.74rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--ink-soft); font-weight: 600; background: #FAFAF7; }
tr:last-child td { border-bottom: none; }
td.num, th.num { text-align: right; }
.empty-row td { text-align: center; color: var(--ink-soft); padding: 34px 20px; }

.badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 999px;
    font-size: 0.74rem;
    font-weight: 600;
}
.badge.ok { background: var(--primary-tint); color: var(--primary-dark); }
.badge.low { background: var(--accent-tint); color: var(--accent); }
.badge.out { background: var(--danger-tint); color: var(--danger); }
.badge.pending { background: #FCEFD2; color: #8A6116; }
.badge.confirmed { background: var(--primary-tint); color: var(--primary-dark); }
.badge.fulfilled { background: var(--primary-tint); color: var(--primary-dark); }
.badge.cancelled { background: var(--danger-tint); color: var(--danger); }

/* ---------- Product grid (seller/customer) ---------- */
.product-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 16px;
    margin-bottom: 10px;
}
.product-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}
.product-card .photo {
    aspect-ratio: 4/3;
    background: var(--paper) center/cover no-repeat;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--ink-soft);
    font-size: 0.8rem;
}
.product-card .body { padding: 14px 16px; display: flex; flex-direction: column; gap: 6px; flex: 1; }
.product-card .name { font-weight: 600; font-size: 0.96rem; }
.product-card .cat { font-size: 0.78rem; color: var(--ink-soft); }
.product-card .price { font-family: 'Fraunces', serif; font-size: 1.15rem; margin-top: auto; }
.product-card .foot { padding: 0 16px 16px; }

.search-row { display: flex; gap: 10px; margin-bottom: 18px; flex-wrap: wrap; }
.search-row input, .search-row select { max-width: 240px; }

/* ---------- Modal ---------- */
.modal-backdrop {
    position: fixed; inset: 0;
    background: rgba(28,35,33,0.45);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 60;
    padding: 20px;
}
.modal-backdrop.open { display: flex; }
.modal {
    background: #fff;
    border-radius: var(--radius);
    max-width: 460px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    padding: 24px;
}
.modal h2 { font-size: 1.15rem; }
.modal-close {
    float: right;
    background: none;
    border: none;
    font-size: 1.3rem;
    cursor: pointer;
    color: var(--ink-soft);
    line-height: 1;
}

/* ---------- AI widget ---------- */
#ai-toggle {
    position: fixed;
    right: 24px;
    bottom: 24px;
    width: 54px;
    height: 54px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    border: none;
    box-shadow: 0 6px 18px rgba(28,35,33,0.25);
    cursor: pointer;
    font-size: 1.4rem;
    z-index: 70;
    display: flex;
    align-items: center;
    justify-content: center;
}
#ai-toggle:hover { background: var(--primary-dark); }

#ai-panel {
    position: fixed;
    right: 24px;
    bottom: 90px;
    width: 340px;
    max-width: calc(100vw - 32px);
    height: 460px;
    max-height: calc(100vh - 130px);
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 14px;
    box-shadow: 0 12px 32px rgba(28,35,33,0.22);
    display: none;
    flex-direction: column;
    overflow: hidden;
    z-index: 70;
}
#ai-panel.open { display: flex; }
#ai-panel .ai-head {
    background: var(--primary);
    color: #fff;
    padding: 12px 16px;
    font-weight: 600;
    font-size: 0.92rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
#ai-panel .ai-head span.sub { font-weight: 400; font-size: 0.75rem; opacity: 0.85; display: block; }
#ai-panel .ai-head button { background: none; border: none; color: #fff; cursor: pointer; font-size: 1.1rem; }
#ai-messages {
    flex: 1;
    overflow-y: auto;
    padding: 14px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    font-size: 0.87rem;
}
.ai-msg { max-width: 85%; padding: 8px 12px; border-radius: 12px; line-height: 1.4; }
.ai-msg.user { align-self: flex-end; background: var(--primary); color: #fff; border-bottom-right-radius: 3px; white-space: pre-wrap; }
.ai-msg.bot  { align-self: flex-start; background: var(--paper); color: var(--ink); border-bottom-left-radius: 3px; }
.ai-msg.note { align-self: center; color: var(--ink-soft); font-size: 0.78rem; text-align: center; white-space: pre-wrap; }

.ai-product-card {
    display: inline-flex; align-items: center; gap: 6px; margin: 3px 2px;
    padding: 3px 10px 3px 3px; border-radius: 999px; background: var(--surface, #eef0f3);
    border: 1px solid var(--border, #dadde2); vertical-align: middle;
}
.ai-product-thumb { width: 22px; height: 22px; border-radius: 50%; object-fit: cover; display: block; }
.ai-product-name { font-weight: 600; font-size: 0.85em; }
#ai-form { display: flex; border-top: 1px solid var(--border); padding: 10px; gap: 8px; }
#ai-form input { flex: 1; border-radius: 20px; }
#ai-form button { border-radius: 50%; width: 38px; height: 38px; padding: 0; flex-shrink: 0; }

.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
@media (max-width: 720px) { .two-col { grid-template-columns: 1fr; } }

.muted { color: var(--ink-soft); }
.mt-0 { margin-top: 0; }

/* ---------- Notifications ---------- */
.notif-wrap { position: relative; }
.notif-bell {
    position: relative;
    width: 40px; height: 40px;
    border-radius: 50%;
    border: 1px solid var(--border);
    background: var(--surface);
    cursor: pointer;
    font-size: 1.1rem;
}
.notif-dot {
    position: absolute;
    top: -4px; right: -4px;
    background: var(--accent);
    color: #fff;
    font-size: 0.66rem;
    font-weight: 700;
    min-width: 16px;
    height: 16px;
    border-radius: 999px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 3px;
}
.notif-dropdown {
    display: none;
    position: absolute;
    right: 0;
    top: 48px;
    width: 300px;
    max-width: calc(100vw - 40px);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 12px 28px rgba(28,35,33,0.18);
    z-index: 65;
    overflow: hidden;
}
.notif-dropdown.open { display: block; }
.notif-title { font-weight: 600; font-size: 0.85rem; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.notif-empty { padding: 18px 14px; color: var(--ink-soft); font-size: 0.85rem; }
.notif-item { display: block; padding: 10px 14px; border-bottom: 1px solid var(--border); font-size: 0.85rem; color: var(--ink); }
.notif-item:last-child { border-bottom: none; }
.notif-item:hover { background: var(--paper); text-decoration: none; }
.notif-sub { display: block; color: var(--ink-soft); font-size: 0.76rem; margin-top: 2px; }

/* ---------- Theme toggle ---------- */
.theme-toggle {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.82rem;
    color: #CFCFC8;
    margin-bottom: 12px;
    cursor: pointer;
}
.theme-toggle input { width: auto; }

/* ---------- Dark mode ---------- */
html[data-theme="dark"] {
    --ink: #EDEDE9;
    --ink-soft: #A6ABA8;
    --paper: #14181A;
    --surface: #1D2224;
    --border: #333A3D;
    --primary-tint: #17332B;
    --accent-tint: #3A2417;
    --danger-tint: #3A211E;
}
html[data-theme="dark"] body { background: var(--paper); }
html[data-theme="dark"] th { background: #232A2C; }
html[data-theme="dark"] .product-card .photo { background-color: var(--paper); }
html[data-theme="dark"] input[type="text"],
html[data-theme="dark"] input[type="password"],
html[data-theme="dark"] input[type="email"],
html[data-theme="dark"] input[type="number"],
html[data-theme="dark"] input[type="file"],
html[data-theme="dark"] select,
html[data-theme="dark"] textarea {
    background: #232A2C;
    color: var(--ink);
    border-color: var(--border);
}
html[data-theme="dark"] .btn.secondary { background: #232A2C; }
html[data-theme="dark"] .modal, html[data-theme="dark"] #ai-panel, html[data-theme="dark"] .notif-dropdown { background: var(--surface); }
html[data-theme="dark"] .bar-chart .bar { background: var(--primary); }

/* ---------- Simple bar chart (reports page) ---------- */
.bar-chart {
    display: flex;
    align-items: flex-end;
    gap: 6px;
    height: 140px;
    padding: 10px 4px 0;
    border-bottom: 1px solid var(--border);
}
.bar-chart .col { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; }
.bar-chart .bar { width: 100%; background: var(--primary); border-radius: 3px 3px 0 0; min-height: 2px; }
.bar-chart .col .lbl { font-size: 0.62rem; color: var(--ink-soft); margin-top: 6px; white-space: nowrap; }

/* ---------- Mobile ---------- */
@media (max-width: 860px) {
    .sidebar {
        position: fixed;
        left: 0; top: 0; bottom: 0;
        transform: translateX(-100%);
        transition: transform 0.2s ease;
        z-index: 80;
    }
    .sidebar.open { transform: translateX(0); }
    .main { padding: 20px 16px 80px; }
    .mobile-toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 38px; height: 38px;
        border-radius: 8px;
        border: 1px solid var(--border);
        background: #fff;
        cursor: pointer;
        margin-right: 10px;
    }
    #ai-panel { right: 16px; left: 16px; width: auto; }
    #ai-toggle { right: 16px; }
}
