/* PSX Portfolio - app.css */
:root {
  --dark:      #0F172A;
  --mid:       #1E293B;
  --slate:     #334155;
  --muted:     #64748B;
  --border:    #CBD5E1;
  --light:     #F1F5F9;
  --white:     #FFFFFF;
  --blue:      #3B82F6;
  --teal:      #14B8A6;
  --amber:     #F59E0B;
  --pos:       #16A34A;
  --neg:       #DC2626;
  --pos-bg:    #DCFCE7;
  --neg-bg:    #FEE2E2;
  --warn-bg:   #FEF3C7;
  --sidebar-w: 220px;
  --topbar-h:  52px;
  --radius:    6px;
  --shadow:    0 1px 3px rgba(0,0,0,.12), 0 1px 2px rgba(0,0,0,.08);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 14px; -webkit-text-size-adjust: 100%; }
body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--light); color: var(--dark);
  display: flex; min-height: 100vh; overflow-x: hidden;
}

/* ---- Sidebar ------------------------------------------------ */
.sidebar {
  width: var(--sidebar-w);
  min-height: 100vh;
  background: var(--dark); color: var(--white);
  display: flex; flex-direction: column;
  position: fixed; top: 0; left: 0; z-index: 200;
  transition: transform .25s ease;
  overflow-y: auto;
}
.sidebar-logo {
  display: flex; align-items: center; gap: 10px;
  padding: 16px 14px 12px;
  border-bottom: 1px solid rgba(255,255,255,.08);
  font-size: .95rem; font-weight: 700;
}
.logo-icon { font-size: 1.3rem; flex-shrink: 0; }
.logo-text  { flex: 1; }

/* Close X  hidden on desktop, shown on mobile via media query */
.sidebar-close {
  display: none; /* DESKTOP: hidden */
  background: none; border: none; color: #94A3B8;
  font-size: 1.1rem; cursor: pointer; padding: 4px;
  line-height: 1; flex-shrink: 0;
}

.nav-links { list-style: none; padding: 6px 0; flex: 1; }
.nav-link {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px; color: #94A3B8;
  text-decoration: none; font-size: .85rem; font-weight: 500;
  transition: background .15s, color .15s;
  min-height: 44px;
}
.nav-link:hover  { background: rgba(255,255,255,.07); color: #e2e8f0; }
.nav-link.active { background: var(--blue); color: var(--white); }
.nav-icon { font-size: 1rem; width: 20px; text-align: center; flex-shrink: 0; }
.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid rgba(255,255,255,.08);
  font-size: .72rem; color: #64748b;
}
.sidebar-time { font-variant-numeric: tabular-nums; }

/* Overlay  hidden on desktop, shown when sidebar-open on mobile */
.sidebar-overlay {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,.5);
  z-index: 190;
}

/* ---- Main wrap ---------------------------------------------- */
.main-wrap {
  margin-left: var(--sidebar-w);
  flex: 1; display: flex; flex-direction: column;
  min-height: 100vh;
  /* Critical: prevent content from exceeding the viewport width */
  min-width: 0;
  max-width: calc(100vw - var(--sidebar-w));
  overflow-x: hidden;
}

/* ---- Topbar ------------------------------------------------- */
.topbar {
  height: var(--topbar-h); background: var(--white);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 10px;
  padding: 0 16px;
  position: sticky; top: 0; z-index: 100;
  box-shadow: var(--shadow);
}

/* Hamburger  hidden on desktop */
.sidebar-toggle {
  display: none; /* DESKTOP: hidden */
  background: none; border: none;
  font-size: 1.25rem; cursor: pointer;
  padding: 0; width: 44px; height: 44px;
  align-items: center; justify-content: center;
  flex-shrink: 0;
}

.topbar-title {
  font-size: .95rem; font-weight: 600; flex: 1;
  min-width: 0; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
.topbar-right { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.badge { font-size: .68rem; font-weight: 700; padding: 2px 7px; border-radius: 99px; }
.badge-live { background: var(--pos-bg); color: var(--pos); animation: pulse 2s infinite; }
@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:.6} }
.topbar-refresh-btn { white-space: nowrap; }

/* ---- Page content ------------------------------------------- */
.page-content {
  padding: 16px; flex: 1;
  min-width: 0;
  /* Prevent any child from pushing width beyond viewport */
  overflow-x: hidden;
}

/* ---- KPI Cards --------------------------------------------- */
.kpi-grid { display: grid; gap: 10px; margin-bottom: 16px; }
.kpi-grid-6 { grid-template-columns: repeat(6,1fr); }
.kpi-grid-4 { grid-template-columns: repeat(4,1fr); }
.kpi-grid-3 { grid-template-columns: repeat(3,1fr); }
.kpi-card {
  background: var(--white); border-radius: var(--radius);
  border: 1px solid var(--border); padding: 12px 14px;
  box-shadow: var(--shadow); min-width: 0;
}
.kpi-label { font-size: .65rem; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin-bottom: 5px; }
.kpi-value { font-size: 1.1rem; font-weight: 700; line-height: 1.2; word-break: break-word; }
.kpi-sub   { font-size: .67rem; color: var(--muted); margin-top: 3px; }
.kpi-card.blue   .kpi-value { color: var(--blue); }
.kpi-card.teal   .kpi-value { color: var(--teal); }
.kpi-card.amber  .kpi-value { color: var(--amber); }
.kpi-card.pos    .kpi-value { color: var(--pos); }
.kpi-card.neg    .kpi-value { color: var(--neg); }
.kpi-card.pos-bg { background: var(--pos-bg); }
.kpi-card.neg-bg { background: var(--neg-bg); }

/* ---- Alert banners ------------------------------------------ */
.alert-banner {
  padding: 9px 12px; border-radius: var(--radius);
  font-size: .8rem; font-weight: 600; margin-bottom: 12px;
  word-break: break-word;
}
.alert-banner.ok   { background: var(--pos-bg); color: #166534; border: 1px solid #bbf7d0; }
.alert-banner.warn { background: var(--warn-bg); color: #92400e; border: 1px solid #fde68a; }
.alert-banner.err  { background: var(--neg-bg);  color: #991b1b; border: 1px solid #fecaca; }

/* ---- Refresh bar -------------------------------------------- */
.refresh-bar {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 7px 12px; background: var(--white);
  border: 1px solid var(--border); border-radius: var(--radius);
  margin-bottom: 12px; font-size: .78rem;
}
.refresh-status { font-weight: 600; }
.refresh-status.loading { color: var(--amber); }
.refresh-status.ok      { color: var(--pos); }
.refresh-status.error   { color: var(--neg); }
.refresh-status.closed  { color: #6366f1; }
.refresh-status.warn    { color: var(--amber); }
.refresh-age { color: var(--muted); flex: 1; min-width: 0; font-size: .72rem; }
.market-closed-badge {
  background: #EEF2FF; color: #4F46E5;
  padding: 3px 10px; border-radius: 99px;
  font-size: .72rem; font-weight: 700;
  border: 1px solid #C7D2FE; white-space: nowrap;
}

/* ---- Panels ------------------------------------------------- */
.two-panel    { display: grid; grid-template-columns: 1fr 1fr;    gap: 14px; margin-bottom: 16px; }
.two-panel-60 { display: grid; grid-template-columns: 60% 1fr;   gap: 14px; margin-bottom: 16px; }
.two-panel-40 { display: grid; grid-template-columns: 40% 1fr;   gap: 14px; margin-bottom: 16px; }
.three-panel  { display: grid; grid-template-columns: repeat(3,1fr); gap: 14px; margin-bottom: 16px; }

/* ---- Cards -------------------------------------------------- */
.card {
  background: var(--white); border-radius: var(--radius);
  border: 1px solid var(--border); box-shadow: var(--shadow);
  margin-bottom: 14px;
  /* Prevent card from growing wider than its grid cell */
  min-width: 0; overflow: hidden;
}
.card-header {
  background: var(--mid); color: var(--white);
  padding: 9px 14px; font-size: .82rem; font-weight: 700;
  display: flex; justify-content: space-between; align-items: center;
  flex-wrap: wrap; gap: 6px;
}
.card-header.blue  { background: var(--blue); }
.card-header.teal  { background: var(--teal); }
.card-header.pos   { background: #14532D; }
.card-header.neg   { background: #7F1D1D; }
.card-header.amber { background: #92400E; }
.card-body { padding: 12px 14px; }

/* ---- Tables ------------------------------------------------- */
/*
 * The table containment strategy:
 * .card already has overflow:hidden so nothing bleeds outside the card border.
 * .tbl-wrap scrolls horizontally when the table is wider than available space.
 * We do NOT set min-width on table globally  that causes overflow on all pages.
 * Pages with many columns get a data-min-width attribute or class.
 */
.tbl-wrap {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* Scroll indicator shadow on right edge when table overflows */
.tbl-wrap-shadow {
  background: linear-gradient(to right, white 80%, rgba(203,213,225,.7)) no-repeat right;
  background-size: 40px 100%;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: .78rem;
  /* Table lays out at content width but never wider than container */
  table-layout: auto;
}

thead th {
  background: var(--slate); color: var(--white);
  padding: 8px 10px; text-align: left;
  font-size: .7rem; font-weight: 700;
  white-space: nowrap;
  position: sticky; top: 0; z-index: 1;
}
tbody tr:nth-child(even) { background: var(--light); }
tbody tr:hover { background: #E0F2FE; }
tbody td {
  padding: 7px 10px; border-bottom: 1px solid #f1f5f9;
  white-space: nowrap; vertical-align: middle;
}
.tbl-total td { background: var(--dark); color: var(--white); font-weight: 700; }
.tbl-total td.pos { color: #86EFAC; }
.tbl-total td.neg { color: #FCA5A5; }

/* Wide tables (market, portfolio, EOD)  hint that they scroll */
.tbl-wide table { min-width: 800px; }
.tbl-xwide table { min-width: 1100px; }

/* ---- Color utils -------------------------------------------- */
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.pos-bg  { background: var(--pos-bg)  !important; }
.neg-bg  { background: var(--neg-bg)  !important; }
.warn-bg { background: var(--warn-bg) !important; }
.muted   { color: var(--muted); }
.bold    { font-weight: 700; }
.pending { color: var(--muted); font-style: italic; font-weight: 400; }
.row-holding { background: #EFF6FF !important; }
.row-holding:nth-child(even) { background: #DBEAFE !important; }

/* ---- Tags --------------------------------------------------- */
.tag { display: inline-block; padding: 1px 7px; border-radius: 99px; font-size: .67rem; font-weight: 700; }
.tag-pos  { background: var(--pos-bg); color: var(--pos); }
.tag-neg  { background: var(--neg-bg); color: var(--neg); }
.tag-warn { background: var(--warn-bg); color: #92400e; }
.tag-blue { background: #DBEAFE; color: #1d4ed8; }
.tag-grey { background: var(--light); color: var(--muted); }

/* ---- Forms -------------------------------------------------- */
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px,1fr)); gap: 10px; }
.form-group { display: flex; flex-direction: column; gap: 4px; }
.form-group label { font-size: .73rem; font-weight: 600; color: var(--slate); }
.form-group input,
.form-group select,
.form-group textarea {
  padding: 8px 10px; border: 1px solid var(--border);
  border-radius: var(--radius); font-size: .85rem;
  background: var(--white); color: var(--dark);
  transition: border-color .15s;
  min-height: 40px;
  width: 100%;
}
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  outline: none; border-color: var(--blue);
  box-shadow: 0 0 0 2px rgba(59,130,246,.15);
}
.form-actions { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }

/* ---- Buttons ------------------------------------------------ */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 5px;
  padding: 8px 14px; border-radius: var(--radius);
  font-size: .82rem; font-weight: 600; cursor: pointer;
  border: 1.5px solid transparent; text-decoration: none;
  transition: all .15s; white-space: nowrap; min-height: 36px;
}
.btn-primary { background: var(--blue); color: var(--white); border-color: var(--blue); }
.btn-primary:hover { background: #2563eb; }
.btn-danger  { background: var(--neg);  color: var(--white); border-color: var(--neg); }
.btn-danger:hover  { background: #b91c1c; }
.btn-success { background: var(--pos);  color: var(--white); border-color: var(--pos); }
.btn-outline { background: transparent; color: var(--slate); border-color: var(--border); }
.btn-outline:hover { background: var(--light); }
.btn-sm { padding: 5px 11px; font-size: .76rem; min-height: 32px; }
.btn-xs { padding: 3px 8px;  font-size: .69rem; min-height: 28px; }

/* ---- Alerts ------------------------------------------------- */
.alert { padding: 10px 14px; border-radius: var(--radius); font-size: .82rem; margin-bottom: 14px; }
.alert-success { background: var(--pos-bg); color: #166534; border: 1px solid #bbf7d0; }
.alert-error   { background: var(--neg-bg); color: #991b1b; border: 1px solid #fecaca; }
.alert-info    { background: #DBEAFE;       color: #1e40af; border: 1px solid #bfdbfe; }

/* ---- Filter bar -------------------------------------------- */
.filter-bar { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; align-items: center; }
.filter-bar input {
  padding: 6px 10px; border: 1px solid var(--border);
  border-radius: var(--radius); font-size: .82rem; min-width: 140px; flex: 1;
}

/* ---- EOD heat ---------------------------------------------- */
.heat-5p  { background: #BBFADA; color: #14532D; }
.heat-pos { background: var(--pos-bg); color: var(--pos); }
.heat-neg { background: var(--neg-bg); color: var(--neg); }
.heat-5n  { background: #FECACA; color: #7F1D1D; }

/* ---- Watchlist edit ---------------------------------------- */
.wl-hidden { display: none !important; }
.edit-row td { border-top: 2px solid var(--blue) !important; border-bottom: 2px solid var(--blue) !important; }

/* ---- Misc -------------------------------------------------- */
.section-title { font-size: .75rem; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--slate); border-bottom: 2px solid var(--blue); padding-bottom: 5px; margin-bottom: 10px; }
.spark { width: 100%; height: 40px; }
.pagination { display: flex; gap: 6px; margin-top: 12px; flex-wrap: wrap; }
.pagination a, .pagination span { padding: 5px 10px; border: 1px solid var(--border); border-radius: var(--radius); font-size: .76rem; text-decoration: none; color: var(--slate); }
.pagination a:hover { background: var(--light); }
.pagination .active { background: var(--blue); color: var(--white); border-color: var(--blue); }
.range-bar      { position: relative; height: 6px; background: var(--border); border-radius: 3px; width: 80px; display: inline-block; vertical-align: middle; }
.range-bar-fill { position: absolute; height: 100%; background: var(--blue); border-radius: 3px; }

/* ==============================================================
   RESPONSIVE BREAKPOINTS
   ============================================================== */

/* ---- Tablet: <= 1200px ------------------------------------- */
@media (max-width: 1200px) {
  .kpi-grid-6 { grid-template-columns: repeat(3, 1fr); }
}

/* ---- Tablet: <= 1024px ------------------------------------- */
@media (max-width: 1024px) {
  .two-panel-60 { grid-template-columns: 1fr; }
  .kpi-grid-6   { grid-template-columns: repeat(3, 1fr); }
}

/* ---- <= 900px: Mobile layout (sidebar off-canvas) ---------- */
@media (max-width: 900px) {

  /* Sidebar: off-canvas, slides in */
  .sidebar { transform: translateX(-100%); box-shadow: none; }
  body.sidebar-open .sidebar { transform: translateX(0); box-shadow: 4px 0 24px rgba(0,0,0,.3); }

  /* Overlay appears when sidebar open */
  body.sidebar-open .sidebar-overlay { display: block; }

  /* Show X inside sidebar on mobile ONLY */
  .sidebar-close { display: flex; align-items: center; justify-content: center; }

  /* Show hamburger in topbar on mobile ONLY */
  .sidebar-toggle { display: flex; }

  /* Main: full width */
  .main-wrap { margin-left: 0; max-width: 100vw; }

  /* KPI */
  .kpi-grid-6 { grid-template-columns: repeat(2, 1fr); }
  .kpi-grid-4 { grid-template-columns: repeat(2, 1fr); }
  .kpi-grid-3 { grid-template-columns: repeat(2, 1fr); }

  /* Panels stack */
  .two-panel, .two-panel-60, .two-panel-40, .three-panel { grid-template-columns: 1fr; }

  /* Topbar */
  .badge-live { display: none; }
  .topbar-refresh-btn { font-size: .72rem; padding: 5px 10px; }
}

/* ---- <= 600px: Phone --------------------------------------- */
@media (max-width: 600px) {
  .page-content { padding: 10px; }
  .kpi-grid-6, .kpi-grid-4, .kpi-grid-3 { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .kpi-card  { padding: 9px 10px; }
  .kpi-value { font-size: .9rem; }
  .kpi-label { font-size: .6rem; }
  .card-header { font-size: .78rem; padding: 8px 10px; }
  .card-body   { padding: 10px; }
  .refresh-bar { padding: 6px 10px; font-size: .72rem; }
  .topbar { padding: 0 10px; gap: 6px; }
  .topbar-title { font-size: .85rem; }
  .alert-banner { font-size: .75rem; padding: 8px 10px; }
  .filter-bar input { min-width: 100%; }
  .form-grid { grid-template-columns: 1fr; }
  /* Table font smaller on phones */
  tbody td, thead th { padding: 5px 8px; font-size: .71rem; }
  /* Wide/xwide tables need narrower min on phone */
  .tbl-wide table  { min-width: 600px; }
  .tbl-xwide table { min-width: 700px; }
}

/* ---- <= 380px: Very small phone ---------------------------- */
@media (max-width: 380px) {
  .kpi-grid-6, .kpi-grid-4, .kpi-grid-3 { grid-template-columns: 1fr 1fr; }
  .kpi-value { font-size: .85rem; }
  .topbar-refresh-btn { padding: 4px 8px; font-size: .67rem; }
}
