:root{
  --bg: #0b1220;
  --panel: rgba(255,255,255,.06);
  --panel2: rgba(255,255,255,.08);
  --text: rgba(255,255,255,.92);
  --muted: rgba(255,255,255,.70);
  --border: rgba(255,255,255,.10);
  --primary: #0ea5e9;
  --primary2:#2563eb;
  --danger:#ef4444;
  --ok:#22c55e;
  --warn:#f59e0b;
  --shadow: 0 14px 50px rgba(0,0,0,.40);
  --radius: 16px;
  --radius2: 22px;
  --font: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji","Segoe UI Emoji";
}

@media (prefers-color-scheme: light){
  :root{
    --bg:#f7fafc;
    --panel:#ffffff;
    --panel2:#f2f4f7;
    --text:#0b1220;
    --muted: rgba(11,18,32,.70);
    --border: rgba(11,18,32,.12);
    --shadow: 0 14px 50px rgba(15,23,42,.15);
  }
}

*{ box-sizing:border-box; }
html,body{ height:100%; }
body{
  margin:0;
  font-family:var(--font);
  background: radial-gradient(1200px 600px at 20% -10%, rgba(14,165,233,.25), transparent 55%),
              radial-gradient(900px 500px at 120% 10%, rgba(37,99,235,.20), transparent 60%),
              var(--bg);
  background-repeat: no-repeat;
  color:var(--text);
}

a{ color:inherit; text-decoration:none; }

.app-shell{ min-height:100%; }

/* Layout */
.shell{
  display:grid;
  grid-template-columns: 280px 1fr;
  min-height:100vh;
  transition: grid-template-columns .22s ease;
}

.sidebar{
  position:sticky;
  top:0;
  height:100vh;
  padding:18px;
  border-right:1px solid var(--border);
  background: linear-gradient(180deg, rgba(255,255,255,.06), rgba(255,255,255,.03));
  backdrop-filter: blur(14px);
  transition: padding .22s ease;
}

.sidebar-backdrop{ display:none; }

.brand{
  display:flex;
  align-items:center;
  gap:12px;
  padding:12px;
  border:1px solid var(--border);
  border-radius: var(--radius2);
  background: rgba(255,255,255,.06);
  box-shadow: var(--shadow);
}
.brand img{ width:38px; height:38px; }
.brand .title{ font-weight:800; letter-spacing:.2px; }
.brand .subtitle{ font-size:12px; color:var(--muted); margin-top:2px; }

.sidebar-meta{
  margin-top:14px;
  padding:12px;
  border:1px solid var(--border);
  border-radius: var(--radius2);
  background: rgba(255,255,255,.05);
}
.sidebar-meta-value{
  font-weight:800;
  font-size:12px;
  word-break:break-all;
  opacity:.9;
}

.nav{
  margin-top:16px;
  display:flex;
  flex-direction:column;
  gap:8px;
}

.nav a{
  display:flex;
  align-items:center;
  gap:10px;
  padding:12px 12px;
  border:1px solid transparent;
  border-radius: 14px;
  color:var(--muted);
}

.nav a:hover{ background: rgba(255,255,255,.06); color:var(--text); border-color: var(--border); }

.nav a.active{
  background: linear-gradient(90deg, rgba(14,165,233,.22), rgba(37,99,235,.14));
  border-color: rgba(14,165,233,.35);
  color: var(--text);
}

.nav .group-label{
  margin:14px 8px 4px;
  font-size:12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .10em;
}

.shell.sidebar-collapsed{
  grid-template-columns: 92px 1fr;
}
.shell.sidebar-collapsed .brand{
  justify-content:center;
  padding:10px;
}
.shell.sidebar-collapsed .brand .brand-copy,
.shell.sidebar-collapsed .nav .group-label,
.shell.sidebar-collapsed .nav .nav-text,
.shell.sidebar-collapsed .sidebar-meta{
  display:none;
}
.shell.sidebar-collapsed .nav a{
  justify-content:center;
  padding-inline:10px;
}

.main{
  padding:18px 22px 28px;
  min-width: 0;
}

.header{
  display:flex;
  align-items:center;
  justify-content: space-between;
  gap:12px;
  padding:14px 16px;
  border:1px solid var(--border);
  border-radius: var(--radius2);
  background: rgba(255,255,255,.06);
  backdrop-filter: blur(14px);
  box-shadow: var(--shadow);
}

.header .left{ display:flex; align-items:center; gap:10px; }
.header .crumb{ color: var(--muted); font-size: 13px; }
.header .page-title{ font-weight: 800; font-size: 16px; }

.user-pill{
  display:flex;
  align-items:center;
  gap:10px;
  padding:10px 12px;
  border:1px solid var(--border);
  border-radius: 999px;
  background: rgba(255,255,255,.06);
}

.avatar{
  width:30px; height:30px;
  border-radius: 999px;
  display:grid;
  place-items:center;
  background: linear-gradient(135deg, rgba(14,165,233,.35), rgba(37,99,235,.20));
  border: 1px solid rgba(14,165,233,.35);
  font-weight: 800;
}

.btn{
  display:inline-flex;
  align-items:center;
  justify-content:center;
  gap:8px;
  padding:10px 14px;
  border-radius: 14px;
  border:1px solid var(--border);
  background: rgba(255,255,255,.06);
  color: var(--text);
  cursor:pointer;
  user-select:none;
}

.btn:hover{ background: rgba(255,255,255,.10); }
.btn.icon-btn{ width:40px; height:40px; padding:0; }

.btn.primary{ background: linear-gradient(90deg, rgba(14,165,233,.85), rgba(37,99,235,.75)); border-color: rgba(14,165,233,.45); }
.btn.primary:hover{ filter: brightness(1.05); }
.btn.danger{ background: rgba(239,68,68,.16); border-color: rgba(239,68,68,.35); }

.content{
  margin-top: 16px;
  display:flex;
  flex-direction:column;
  gap: 16px;
  min-width: 0;
}

.card{
  border:1px solid var(--border);
  background: rgba(255,255,255,.06);
  border-radius: var(--radius2);
  box-shadow: var(--shadow);
  overflow:hidden;
  min-width: 0;
}

.card .card-h{
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:12px;
}

.card .card-h h2{ margin:0; font-size:14px; letter-spacing:.2px; }
.card .card-b{
  padding: 16px;
  min-width: 0;
}

.grid{
  display:grid;
  grid-template-columns: repeat(12, 1fr);
  gap: 12px;
}

.kpi{
  grid-column: span 4;
  padding: 14px;
  border:1px solid var(--border);
  border-radius: var(--radius);
  background: rgba(255,255,255,.05);
}
.kpi .label{ color: var(--muted); font-size: 12px; }
.kpi .value{ font-size: 26px; font-weight: 900; margin-top: 6px; }
.kpi .hint{ color: var(--muted); font-size: 12px; margin-top: 6px; }

.form{
  display:grid;
  grid-template-columns: repeat(12,1fr);
  gap: 12px;
}

.field{ grid-column: span 6; display:flex; flex-direction:column; gap:6px; }
.field.sm{ grid-column: span 3; }
.field.md{ grid-column: span 4; }
.field.lg{ grid-column: span 12; }

.signed-input{
  display:flex;
  align-items:stretch;
  gap:8px;
}

.signed-input input{
  flex:1;
  min-width:0;
}

.sign-buttons{
  display:flex;
  gap:6px;
  flex-shrink:0;
}

.sign-btn{
  min-width:38px;
  padding: 10px 0;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(255,255,255,.05);
  color: var(--text);
  font-weight: 800;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
}

.sign-btn:hover{
  background: rgba(255,255,255,.08);
}

.sign-btn:focus-visible{
  border-color: rgba(14,165,233,.60);
  box-shadow: 0 0 0 3px rgba(14,165,233,.18);
}

label{ font-size:12px; color: var(--muted); }
input, select, textarea{
  width:100%;
  padding: 10px 12px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: rgba(255,255,255,.05);
  color: var(--text);
  outline:none;
}
input:focus, select:focus, textarea:focus{ border-color: rgba(14,165,233,.60); box-shadow: 0 0 0 3px rgba(14,165,233,.18); }
/* Native option popups (notably on Windows) often render with light backgrounds. */
select option{
  color: #0b1220;
  background-color: #ffffff;
}
select option:checked,
select option:hover{
  color: #ffffff;
  background-color: #2563eb;
}
textarea{ min-height: 110px; resize: vertical; }

.section-title{
  margin-top: 6px;
  margin-bottom: 8px;
  font-weight: 900;
  font-size: 13px;
  color: var(--text);
}

.patient-products-panel{
  border:1px solid var(--border);
  border-radius: var(--radius);
  background: rgba(255,255,255,.04);
  padding:16px;
}

.patient-products-header{
  display:flex;
  align-items:flex-start;
  justify-content:space-between;
  gap:12px;
  margin-bottom:12px;
}

.patient-products-summary{
  display:flex;
  flex-direction:column;
  gap:10px;
}

.patient-products-empty{
  padding:16px;
  border:1px dashed var(--border);
  border-radius: 14px;
  background: rgba(255,255,255,.03);
  color: var(--muted);
  text-align:center;
}

.reports-tabs{
  display:flex;
  gap:10px;
  flex-wrap:wrap;
}

.reports-pane{
  display:flex;
  flex-direction:column;
  gap:16px;
}

.reports-card-actions{
  justify-content:flex-end;
}

.reports-stack{
  display:flex;
  flex-direction:column;
  gap:16px;
}

.reports-kpi-grid .kpi{
  grid-column: span 3;
}

.report-notice{
  padding:16px;
  border:1px dashed var(--border);
  border-radius: 14px;
  background: rgba(255,255,255,.03);
}

.report-notice.danger{
  border-style: solid;
}

.report-bars{
  display:flex;
  flex-direction:column;
  gap:12px;
  padding:14px;
  border:1px solid var(--border);
  border-radius: var(--radius);
  background: rgba(255,255,255,.04);
}

.report-bar-row{
  display:flex;
  flex-direction:column;
  gap:8px;
}

.report-bar-meta{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:12px;
  font-size:13px;
}

.report-bar-track{
  width:100%;
  height:10px;
  border-radius:999px;
  background: rgba(255,255,255,.08);
  overflow:hidden;
}

.report-bar-fill{
  height:100%;
  border-radius:999px;
  background: linear-gradient(90deg, rgba(14,165,233,.92), rgba(37,99,235,.76));
}

.patient-products-table-wrap table{
  min-width: max(100%, 780px);
}

.patient-products-qty{
  min-width:84px;
  width:84px;
}

.product-picker-dialog{
  width:min(980px, calc(100vw - 24px));
  max-width:980px;
  max-height:min(86vh, 920px);
  padding:0;
  border:1px solid var(--border);
  border-radius: 24px;
  background: rgba(11,18,32,.94);
  color: var(--text);
  box-shadow: var(--shadow);
}

.product-picker-dialog::backdrop{
  background: rgba(2,6,23,.70);
  backdrop-filter: blur(6px);
}

.product-picker-sheet{
  display:flex;
  flex-direction:column;
  gap:14px;
  padding:18px;
}

.product-picker-head{
  display:flex;
  align-items:flex-start;
  justify-content:space-between;
  gap:12px;
}

.product-picker-head h3{
  margin:0;
  font-size:16px;
}

.product-picker-toolbar{
  display:flex;
  align-items:flex-end;
  gap:12px;
  flex-wrap:wrap;
}

.table-wrap{
  overflow-x: auto;
  overflow-y: auto;
  max-width: 100%;
  border-radius: var(--radius);
  border:1px solid var(--border);
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-x: contain;
}

table{
  width: max-content;
  border-collapse: collapse;
  min-width: max(100%, 720px);
}
th,td{ padding: 10px 12px; border-bottom:1px solid var(--border); text-align:left; font-size: 13px; }
th{ color: var(--muted); font-weight:700; background: rgba(255,255,255,.04); position: sticky; top: 0; }
tr:hover td{ background: rgba(255,255,255,.03); }

.badge{ display:inline-flex; align-items:center; gap:6px; padding: 4px 10px; border-radius: 999px; border:1px solid var(--border); font-size: 12px; color: var(--muted); }
.badge.ok{ background: rgba(34,197,94,.12); border-color: rgba(34,197,94,.26); color: rgba(34,197,94,.95); }
.badge.warn{ background: rgba(245,158,11,.12); border-color: rgba(245,158,11,.26); color: rgba(245,158,11,.95); }
.badge.danger{ background: rgba(239,68,68,.12); border-color: rgba(239,68,68,.26); color: rgba(239,68,68,.95); }
.chip-list{ display:flex; flex-wrap:wrap; gap:6px; }
.chip{
  display:inline-flex;
  align-items:center;
  padding:4px 10px;
  border-radius:999px;
  border:1px solid var(--border);
  background: rgba(14,165,233,.12);
  color: var(--text);
  font-size:12px;
  white-space:nowrap;
}
.checkbox-list{
  display:grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap:8px 12px;
  border:1px solid var(--border);
  border-radius: 14px;
  background: rgba(255,255,255,.04);
  padding:10px 12px;
}
.checkbox-item{
  display:flex;
  align-items:center;
  gap:8px;
  font-size:13px;
  color:var(--text);
}
.checkbox-item input[type="checkbox"]{
  width:16px;
  height:16px;
}
.checkbox-item input[type="radio"]{
  width:16px;
  height:16px;
}
.clinic-select-list{
  display:grid;
  grid-template-columns: 1fr;
  gap:8px;
  border:1px solid var(--border);
  border-radius: 14px;
  background: rgba(255,255,255,.04);
  padding:10px 12px;
}
.clinic-select-item{
  padding:6px 2px;
}

.row{ display:flex; gap:12px; flex-wrap:wrap; align-items:center; }
.row.end{ justify-content:flex-end; }

.toast-host{ position: fixed; top: 18px; right: 18px; display:flex; flex-direction:column; gap:10px; z-index: 9999; }
.toast{ min-width: 240px; max-width: 360px; padding: 12px 14px; border-radius: 16px; border:1px solid var(--border); background: rgba(15,23,42,.72); backdrop-filter: blur(12px); box-shadow: var(--shadow); }
.toast .t{ font-weight: 800; font-size: 13px; }
.toast .m{ color: var(--muted); font-size: 12px; margin-top: 4px; }
.toast.ok{ border-color: rgba(34,197,94,.35); }
.toast.err{ border-color: rgba(239,68,68,.35); }

/* Login */
.login{
  min-height:100vh;
  display:grid;
  place-items:center;
  padding: 18px;
}

.login-card{
  width: min(480px, 100%);
  border:1px solid var(--border);
  border-radius: 28px;
  background: rgba(255,255,255,.07);
  backdrop-filter: blur(16px);
  box-shadow: var(--shadow);
  overflow:hidden;
}

.login-card .top{
  padding: 18px 18px 10px;
  display:flex;
  gap: 12px;
  align-items:center;
  border-bottom: 1px solid var(--border);
}

.login-card .top img{ width:44px; height:44px; }
.login-card .top .h1{ font-weight: 900; }
.login-card .top .h2{ color: var(--muted); font-size: 12px; margin-top: 2px; }
.login-card .body{ padding: 18px; }

.small{ font-size: 12px; color: var(--muted); }

/* Responsive */
@media (max-width: 980px){
  .shell{
    grid-template-columns: 1fr;
    position: relative;
  }
  .shell.sidebar-collapsed{ grid-template-columns: 1fr; }

  .sidebar{
    position: fixed;
    top: 0;
    left: 0;
    width: min(86vw, 320px);
    max-width: 320px;
    height: 100dvh;
    border-right:1px solid var(--border);
    border-bottom-right-radius: var(--radius2);
    box-shadow: var(--shadow);
    overflow-y: auto;
    z-index: 40;
    transform: translateX(calc(-100% - 20px));
    opacity: 0;
    pointer-events: none;
    transition: transform .22s ease, opacity .22s ease;
  }
  .shell.sidebar-open .sidebar{
    transform: translateX(0);
    opacity: 1;
    pointer-events: auto;
  }

  .sidebar-backdrop{
    position: fixed;
    inset: 0;
    border: 0;
    padding: 0;
    margin: 0;
    background: rgba(2,6,23,.55);
    opacity: 0;
    pointer-events: none;
    transition: opacity .22s ease;
    z-index: 30;
    display: block;
  }
  .shell.sidebar-open .sidebar-backdrop{
    opacity: 1;
    pointer-events: auto;
  }

  .main{ padding: 14px 14px 24px; }

  table{ min-width: max(100%, 640px); }
  .kpi{ grid-column: span 6; }
  .reports-kpi-grid .kpi{ grid-column: span 6; }
}
@media (max-width: 640px){
  .kpi{ grid-column: span 12; }
  .reports-kpi-grid .kpi{ grid-column: span 12; }
  .field{ grid-column: span 12; }
  .field.sm, .field.md{ grid-column: span 6; }
  .field.pair-field{ grid-column: span 6; }
  .patient-products-header{
    flex-direction:column;
    align-items:stretch;
  }
  .product-picker-dialog{
    width:calc(100vw - 16px);
  }
  .product-picker-sheet{
    padding:14px;
  }
  .product-picker-toolbar{
    flex-direction:column;
    align-items:stretch;
  }
  .report-bar-meta{
    flex-direction:column;
    align-items:flex-start;
  }
  .reports-card-actions{
    width:100%;
    justify-content:flex-start;
  }
}
