/* =========================================================================
   AgriStack — Global stylesheet
   Design system per specification §66 (do not hard-code styles repeatedly).
   ========================================================================= */

:root {
    /* Brief-defined tokens */
    --primary:    #377dff;
    --text:       #172033;
    --muted:      #6b7280;
    --border:     #e5e7eb;
    --surface:    #ffffff;
    --background: #f7f8fa;
    --radius:     12px;

    /* Derived / supporting tokens */
    --primary-600: #2f6ae0;
    --primary-050: #eef4ff;
    --success:     #16a34a;
    --success-bg:  #e9f7ef;
    --danger:      #dc2626;
    --danger-bg:   #fdecec;
    --warning:     #d97706;
    --warning-bg:  #fef3e2;
    --info:        #0ea5e9;

    --radius-sm: 8px;
    --radius-lg: 16px;

    --shadow-xs: 0 1px 2px rgba(23, 32, 51, .05);
    --shadow-sm: 0 1px 3px rgba(23, 32, 51, .07), 0 1px 2px rgba(23, 32, 51, .04);
    --shadow-md: 0 6px 18px rgba(23, 32, 51, .08);

    --sidebar-w: 264px;
    --topbar-h: 64px;

    --font-sans: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

* { box-sizing: border-box; }

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

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

/* ------------------------------------------------------------------ Shell */

.app-shell { display: flex; min-height: 100vh; align-items: stretch; }

.app-main {
    flex: 1 1 auto;
    min-width: 0;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

/* --------------------------------------------------------------- Sidebar */

.app-sidebar {
    width: var(--sidebar-w);
    --bs-offcanvas-width: var(--sidebar-w);
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 0;
}

@media (min-width: 992px) {
    .app-sidebar {
        flex: 0 0 var(--sidebar-w);
        position: sticky;
        top: 0;
        height: 100vh;
    }
}

.sidebar-brand {
    height: var(--topbar-h);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 0 18px;
    border-bottom: 1px solid var(--border);
    flex: 0 0 auto;
}
.brand-link { display: flex; align-items: center; gap: 10px; color: var(--text); font-weight: 700; font-size: 16px; }
.brand-link:hover { color: var(--text); }
.brand-mark {
    width: 34px; height: 34px; border-radius: 9px;
    display: grid; place-items: center;
    background: var(--primary); color: #fff; font-size: 16px;
}

.sidebar-nav {
    flex: 1 1 auto;
    overflow-y: auto;
    padding: 12px 12px 16px;
}
.sidebar-nav::-webkit-scrollbar { width: 8px; }
.sidebar-nav::-webkit-scrollbar-thumb { background: #e2e6ee; border-radius: 8px; }

.nav-group-label {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: #9aa2b1;
    padding: 16px 12px 6px;
}

.sidebar-nav .nav-link {
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 9px 12px;
    margin: 1px 0;
    border-radius: var(--radius-sm);
    color: #48505f;
    font-weight: 500;
    font-size: 13.5px;
    position: relative;
    transition: background-color .12s ease, color .12s ease;
}
.sidebar-nav .nav-link i { width: 18px; text-align: center; font-size: 14px; color: #97a0af; transition: color .12s ease; }
.sidebar-nav .nav-link:hover { background: #f3f5f9; color: var(--text); }
.sidebar-nav .nav-link:hover i { color: #6b7280; }
.sidebar-nav .nav-link.active { background: var(--primary-050); color: var(--primary-600); }
.sidebar-nav .nav-link.active i { color: var(--primary); }
.sidebar-nav .nav-link.active::before {
    content: ""; position: absolute; left: -12px; top: 6px; bottom: 6px;
    width: 3px; border-radius: 0 3px 3px 0; background: var(--primary);
}

.sidebar-footer { padding: 12px 18px 18px; border-top: 1px solid var(--border); flex: 0 0 auto; }
.badge-module {
    display: inline-block; font-size: 11px; font-weight: 600; color: var(--muted);
    background: var(--background); border: 1px solid var(--border);
    padding: 4px 10px; border-radius: 999px;
}

/* --------------------------------------------------------------- Topbar */

.app-topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    height: var(--topbar-h);
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 20px;
    background: rgba(255, 255, 255, .9);
    backdrop-filter: saturate(180%) blur(8px);
    border-bottom: 1px solid var(--border);
}

.btn-icon {
    width: 40px; height: 40px; padding: 0;
    display: inline-grid; place-items: center;
    border: 1px solid transparent; border-radius: var(--radius-sm);
    color: #4b5563; background: transparent;
}
.btn-icon:hover { background: #f3f5f9; color: var(--text); }

.topbar-search {
    position: relative;
    flex: 1 1 auto;
    max-width: 520px;
}
.topbar-search i {
    position: absolute; left: 14px; top: 50%; transform: translateY(-50%);
    color: #9aa2b1; font-size: 13px; pointer-events: none;
}
.topbar-search .form-control {
    height: 40px;
    padding-left: 38px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--background);
    font-size: 13.5px;
}
.topbar-search .form-control:focus {
    background: #fff;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(55, 125, 255, .15);
}

.topbar-actions .notif-badge {
    position: absolute; top: 4px; right: 4px;
    min-width: 17px; height: 17px; padding: 0 4px;
    background: var(--danger); color: #fff;
    font-size: 10px; font-weight: 700; line-height: 17px;
    border-radius: 999px; text-align: center;
}

.user-menu-btn {
    display: flex; align-items: center; gap: 8px;
    padding: 5px 8px; border-radius: var(--radius-sm);
    border: 1px solid transparent; background: transparent; color: var(--text);
}
.user-menu-btn:hover { background: #f3f5f9; }
.user-avatar {
    width: 32px; height: 32px; border-radius: 50%;
    display: inline-grid; place-items: center; overflow: hidden;
    background: var(--primary-050); color: var(--primary); font-size: 13px;
}
.user-avatar img { width: 100%; height: 100%; object-fit: cover; }
.user-name { font-size: 13.5px; font-weight: 600; }

/* Notification dropdown */
.notif-menu { width: 340px; padding: 0; border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-md); overflow: hidden; }
.notif-menu-head { padding: 12px 16px; font-weight: 600; border-bottom: 1px solid var(--border); }
.notif-menu-body { max-height: 340px; overflow-y: auto; }
.notif-item { display: block; padding: 11px 16px; border-bottom: 1px solid #f1f3f7; color: var(--text); }
.notif-item:hover { background: #f7f9fc; color: var(--text); }
.notif-item.unread { background: var(--primary-050); }
.notif-title { display: block; font-weight: 600; font-size: 13px; }
.notif-text { display: block; font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.notif-time { display: block; font-size: 11.5px; color: #9aa2b1; margin-top: 3px; }
.notif-empty { text-align: center; padding: 28px 16px; color: var(--muted); }
.notif-empty i { font-size: 22px; color: #c3c9d4; }
.notif-empty p { margin: 8px 0 0; font-size: 13px; }
.notif-menu-foot { padding: 10px 16px; text-align: center; border-top: 1px solid var(--border); }
.notif-menu-foot a { font-size: 13px; font-weight: 500; }

/* -------------------------------------------------------------- Content */

.app-content { flex: 1 1 auto; padding: 22px; }
.content-container { max-width: 1360px; margin: 0 auto; }

.app-breadcrumb { margin-bottom: 8px; }
.app-breadcrumb .breadcrumb { font-size: 12.5px; }
.app-breadcrumb .breadcrumb-item + .breadcrumb-item::before { content: "/"; color: #c3c9d4; }
.app-breadcrumb .breadcrumb-item.active { color: var(--muted); }

.page-header { margin-bottom: 20px; }
.page-title { font-size: 22px; font-weight: 700; margin: 0; letter-spacing: -.01em; }
.page-description { color: var(--muted); margin: 4px 0 0; font-size: 13.5px; }

/* Demo banner */
.demo-banner {
    display: flex; gap: 12px; align-items: flex-start;
    background: #fff; border: 1px solid var(--border); border-left: 3px solid var(--primary);
    border-radius: var(--radius); padding: 14px 16px; margin-bottom: 20px;
    box-shadow: var(--shadow-xs); font-size: 13.5px; color: #414b5a;
}
.demo-banner i { color: var(--primary); margin-top: 2px; }
.demo-banner strong { color: var(--text); }

/* -------------------------------------------------------------- Panels */

.panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-xs);
    margin-bottom: 20px;
    overflow: hidden;
}
.panel.h-100 { margin-bottom: 0; }
.panel-head {
    display: flex; align-items: center; justify-content: space-between; gap: 10px;
    padding: 14px 18px; border-bottom: 1px solid var(--border);
}
.panel-title { font-size: 15px; font-weight: 600; margin: 0; }
.panel-body { padding: 18px; }

.badge-demo {
    font-size: 11px; font-weight: 600; color: var(--warning);
    background: var(--warning-bg); padding: 3px 9px; border-radius: 999px;
}

/* ---------------------------------------------------------------- KPIs */

.kpi-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 16px;
    margin-bottom: 20px;
}
.kpi-card {
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    box-shadow: var(--shadow-xs); padding: 16px; display: flex; gap: 14px; align-items: center;
}
.kpi-icon {
    width: 44px; height: 44px; border-radius: 11px; flex: 0 0 auto;
    display: grid; place-items: center; font-size: 17px;
    background: var(--primary-050); color: var(--primary);
}
.kpi-label { font-size: 12.5px; color: var(--muted); font-weight: 500; }
.kpi-value { font-size: 24px; font-weight: 700; line-height: 1.15; letter-spacing: -.01em; }
.kpi-hint { font-size: 11px; color: #9aa2b1; margin-top: 1px; }

/* ------------------------------------------------------- Quick actions */

.quick-actions { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 12px; }
.quick-action {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
    padding: 16px 10px; border: 1px solid var(--border); border-radius: var(--radius);
    color: #414b5a; background: #fff; text-align: center; font-size: 12.5px; font-weight: 500;
    transition: border-color .12s ease, box-shadow .12s ease, color .12s ease, transform .12s ease;
}
.quick-action i { font-size: 18px; color: var(--primary); }
.quick-action:hover { border-color: #cdd6ea; box-shadow: var(--shadow-sm); color: var(--text); transform: translateY(-1px); }

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

table.dataTable { border-collapse: collapse !important; }
.table > :not(caption) > * > * { padding: 11px 12px; }
.table thead th {
    font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em;
    color: var(--muted); font-weight: 600; border-bottom: 1px solid var(--border);
    background: #fbfcfe;
}
.table tbody td { border-bottom: 1px solid #f1f3f7; vertical-align: middle; }
.table-hover tbody tr:hover td { background: #f9fbff; }

.status-pill {
    display: inline-block; font-size: 11.5px; font-weight: 600;
    padding: 3px 10px; border-radius: 999px;
}
.status-ok { color: var(--success); background: var(--success-bg); }
.status-warn { color: var(--warning); background: var(--warning-bg); }
.status-off { color: var(--danger); background: var(--danger-bg); }

/* DataTables controls polish */
.dataTables_wrapper .dataTables_filter input,
.dataTables_wrapper .dataTables_length select {
    border: 1px solid var(--border); border-radius: var(--radius-sm);
    padding: 6px 10px; font-size: 13px;
}
.dataTables_wrapper .dataTables_filter input:focus,
.dataTables_wrapper .dataTables_length select:focus {
    outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(55,125,255,.15);
}
.dataTables_wrapper .dataTables_info { color: var(--muted); font-size: 12.5px; padding-top: 12px; }
.dataTables_wrapper .dataTables_paginate .paginate_button.current {
    background: var(--primary) !important; border-color: var(--primary) !important;
    color: #fff !important; border-radius: var(--radius-sm);
}
.dataTables_wrapper .dataTables_paginate .paginate_button {
    border-radius: var(--radius-sm); font-size: 13px;
}

/* -------------------------------------------------------- Activity feed */

.activity-feed { list-style: none; margin: 0; padding: 0; }
.activity-feed li { display: flex; gap: 12px; padding: 10px 0; border-bottom: 1px solid #f1f3f7; }
.activity-feed li:last-child { border-bottom: 0; }
.activity-feed .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--primary); margin-top: 6px; flex: 0 0 auto; }
.act-text { display: block; font-size: 13px; }
.act-time { display: block; font-size: 11.5px; color: #9aa2b1; margin-top: 2px; }

/* --------------------------------------------------------- Empty states */

.empty-state { text-align: center; padding: 34px 16px; color: var(--muted); }
.empty-state i { font-size: 30px; color: #cbd2de; }
.empty-state p { margin: 12px 0 4px; font-weight: 600; color: var(--text); font-size: 14px; }
.empty-state span { font-size: 12.5px; color: var(--muted); display: block; max-width: 360px; margin: 0 auto; }

/* ------------------------------------------------ Notifications (page) */

.notif-list { list-style: none; margin: 0; padding: 0; }
.notif-list li { border-bottom: 1px solid #f1f3f7; }
.notif-list li:last-child { border-bottom: 0; }
.notif-list li a { display: flex; align-items: flex-start; gap: 13px; padding: 14px 8px; color: var(--text); }
.notif-list li a:hover { background: #f9fbff; }
.notif-list li.unread a { background: var(--primary-050); }
.notif-list-icon { width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center; background: var(--primary-050); color: var(--primary); flex: 0 0 auto; }
.notif-list-main { flex: 1 1 auto; min-width: 0; }
.notif-list-title { display: block; font-weight: 600; font-size: 13.5px; }
.notif-list-text { display: block; font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.notif-list-time { font-size: 11.5px; color: #9aa2b1; white-space: nowrap; }

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

.app-toast {
    min-width: 280px; max-width: 360px;
    border: 1px solid var(--border); border-left-width: 4px;
    border-radius: var(--radius-sm); background: #fff; box-shadow: var(--shadow-md);
}
.app-toast .toast-line { display: flex; align-items: flex-start; gap: 10px; padding: 12px 14px; }
.app-toast .toast-ico { font-size: 15px; margin-top: 1px; }
.app-toast .toast-msg { font-size: 13.5px; color: var(--text); flex: 1 1 auto; }
.app-toast.t-success { border-left-color: var(--success); }
.app-toast.t-success .toast-ico { color: var(--success); }
.app-toast.t-error { border-left-color: var(--danger); }
.app-toast.t-error .toast-ico { color: var(--danger); }
.app-toast.t-info { border-left-color: var(--info); }
.app-toast.t-info .toast-ico { color: var(--info); }
.app-toast.t-warning { border-left-color: var(--warning); }
.app-toast.t-warning .toast-ico { color: var(--warning); }

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

.btn-primary { background: var(--primary); border-color: var(--primary); }
.btn-primary:hover, .btn-primary:focus { background: var(--primary-600); border-color: var(--primary-600); }
.btn { font-size: 13.5px; border-radius: var(--radius-sm); }
.btn:focus-visible { box-shadow: 0 0 0 3px rgba(55,125,255,.25); }

/* ------------------------------------------------------------ Modals */

.modal-content { border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-md); }
.modal-header, .modal-footer { border-color: var(--border); }
.modal-title { font-size: 16px; font-weight: 600; }

/* ------------------------------------------------------- Responsive */

@media (max-width: 1199px) {
    .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .quick-actions { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (max-width: 575px) {
    .app-content { padding: 16px; }
    .kpi-grid { grid-template-columns: 1fr; }
    .quick-actions { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .topbar-search { max-width: none; }
}

/* Accessibility: respect reduced-motion preferences */
@media (prefers-reduced-motion: reduce) {
    * { transition: none !important; animation: none !important; }
}
