/* AI Portfolio — dark navy professional palette */
:root {
  --bg: #0b1c33;
  --bg-2: #0f2440;
  --panel: #122b4d;
  --panel-2: #163357;
  --border: #1f3e6b;
  --border-strong: #2a4f86;
  --text: #f4f7fb;
  --text-dim: #aebcd1;
  --text-mute: #7c8ba6;
  --accent: #4f9dff;
  --accent-strong: #2f7fea;
  --green: #2ecc71;
  --green-soft: rgba(46, 204, 113, 0.15);
  --amber: #f5b041;
  --amber-soft: rgba(245, 176, 65, 0.15);
  --red: #ef5b5b;
  --red-soft: rgba(239, 91, 91, 0.15);
  --shadow: 0 1px 2px rgba(0,0,0,0.25), 0 8px 24px rgba(0,0,0,0.25);
  --radius: 10px;
  --radius-sm: 6px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* Topbar */
.topbar {
  background: linear-gradient(180deg, #0a1a30 0%, #0b1c33 100%);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar-inner {
  max-width: 1040px;
  margin: 0 auto;
  /* iOS PWA safe-area: extend padding under the notch when launched from home screen */
  padding: max(10px, env(safe-area-inset-top)) max(20px, env(safe-area-inset-right)) 10px max(20px, env(safe-area-inset-left));
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  width: 36px; height: 36px;
  border-radius: 8px;
  background: linear-gradient(135deg, #2f7fea, #4f9dff);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 14px; letter-spacing: 0.5px;
}
.brand-title { font-weight: 600; font-size: 15px; }
.brand-sub { color: var(--text-mute); font-size: 11.5px; }
.topbar-actions { display: flex; gap: 8px; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border-strong);
  cursor: pointer;
  transition: background 120ms, border-color 120ms;
  font-family: inherit;
}
.btn:hover { background: var(--panel-2); }
.btn-primary {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
  color: #fff;
}
.btn-primary:hover { background: var(--accent); border-color: var(--accent); }
.btn-ghost { border-color: transparent; color: var(--text-dim); }
.btn-ghost:hover { color: var(--text); background: var(--panel-2); }
.btn-danger-ghost { border-color: rgba(239, 91, 91, 0.4); color: var(--red); }
.btn-danger-ghost:hover { background: var(--red-soft); }
.btn-icon { font-size: 14px; }

.link {
  background: none;
  border: none;
  color: var(--accent);
  text-decoration: underline;
  cursor: pointer;
  font: inherit;
  padding: 0;
}

/* Banner */
.banner {
  max-width: 1040px;
  margin: 10px auto 0;
  padding: 9px 14px;
  border-radius: var(--radius-sm);
  font-size: 12.5px;
  border: 1px solid;
}
.banner-warn {
  background: var(--amber-soft);
  border-color: rgba(245, 176, 65, 0.45);
  color: #f7c674;
}

/* Container — narrower for portfolio focus */
.container {
  max-width: 1040px;
  margin: 0 auto;
  padding: 14px 20px 40px;
}

/* Summary */
.summary-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  margin-bottom: 14px;
}
.summary-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 14px;
}
.summary-item {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
}
.summary-label {
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-mute);
}
.summary-value {
  font-family: var(--mono);
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.2px;
}
.summary-value-sm {
  font-family: var(--mono);
  font-size: 13px;
  font-weight: 500;
}
.summary-sub {
  font-size: 11.5px;
  color: var(--text-dim);
}

/* Positions card */
.positions-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  gap: 14px;
  flex-wrap: wrap;
}
.card-head-left { display: flex; align-items: baseline; gap: 10px; }
.card-head h2 {
  margin: 0;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.3px;
}
.card-head-sub {
  font-size: 11.5px;
  color: var(--text-mute);
}

/* Filter chips */
.filter-chips {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}
.chip {
  background: transparent;
  border: 1px solid var(--border-strong);
  color: var(--text-dim);
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: all 120ms;
}
.chip:hover { background: var(--panel-2); color: var(--text); }
.chip.active {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
  color: #fff;
}

.table-wrap { overflow-x: auto; }
.positions-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12.5px;
  table-layout: fixed;
}

/* Explicit column widths — denser layout */
.positions-table .col-ticker { width: 80px; }
.positions-table .col-name   { width: auto; }
.positions-table .col-qty    { width: 56px; }
.positions-table .col-cost   { width: 88px; }
.positions-table .col-price  { width: 88px; }
.positions-table .col-value  { width: 92px; }
.positions-table .col-pnl    { width: 96px; }
.positions-table .col-status { width: 86px; }

.positions-table thead th {
  text-align: left;
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.55px;
  color: var(--text-mute);
  font-weight: 600;
  padding: 9px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-2);
  position: sticky;
  top: 0;
}
.positions-table th.num, .positions-table td.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-family: var(--mono);
}
.positions-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.positions-table .col-name {
  max-width: 0; /* flex shrink properly with ellipsis */
}
.positions-table tr.pos-row { cursor: pointer; transition: background 100ms; }
.positions-table tr.pos-row:hover { background: var(--panel-2); }
.positions-table tr.pos-row.expanded { background: var(--panel-2); }
.positions-table tr:last-child td { border-bottom: none; }

/* Sleeve group header */
.positions-table tr.sleeve-head td {
  background: var(--bg-2);
  padding: 8px 12px;
  font-size: 10.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-mute);
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}
.positions-table tr.sleeve-head .sleeve-meta {
  float: right;
  font-family: var(--mono);
  font-weight: 500;
  text-transform: none;
  letter-spacing: 0;
  color: var(--text-dim);
}
.positions-table tr.sleeve-head .sleeve-meta .pnl-pos { color: var(--green); }
.positions-table tr.sleeve-head .sleeve-meta .pnl-neg { color: var(--red); }

.ticker { font-weight: 600; font-family: var(--mono); font-size: 12.5px; }
.isin { color: var(--text-mute); font-size: 10px; font-family: var(--mono); margin-top: 1px; }
.name { color: var(--text-dim); font-size: 12.5px; }

.pnl-pos { color: var(--green); }
.pnl-neg { color: var(--red); }
.pnl-flat { color: var(--text-dim); }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.4px;
  text-transform: uppercase;
  border: 1px solid;
}
.badge-ok { color: var(--green); background: var(--green-soft); border-color: rgba(46,204,113,0.4); }
.badge-watch { color: var(--amber); background: var(--amber-soft); border-color: rgba(245,176,65,0.4); }
.badge-triggered { color: var(--red); background: var(--red-soft); border-color: rgba(239,91,91,0.5); }
.badge-planned { color: #9aa8be; background: rgba(154,168,190,0.10); border-color: rgba(154,168,190,0.35); }

.positions-table tr.pos-row.planned td:not(:first-child):not(:last-child) { opacity: 0.55; }
.positions-table tr.pos-row.planned .ticker { opacity: 0.85; }

.sparkline { display: none; }

/* Expanded row */
tr.detail-row td {
  background: var(--bg-2);
  padding: 0;
  border-bottom: 1px solid var(--border);
}
.detail-inner {
  padding: 14px 18px 18px;
  display: grid;
  grid-template-columns: 2fr 1fr 1fr;
  gap: 18px;
}
.detail-block .detail-label {
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-mute);
  margin-bottom: 4px;
}
.detail-block .detail-value {
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1.5;
}
.detail-actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  grid-column: 1 / -1;
}

/* Footer */
.footer {
  text-align: center;
  color: var(--text-mute);
  font-size: 11.5px;
  margin-top: 24px;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(4, 12, 24, 0.6);
  backdrop-filter: blur(2px);
}
.modal-card {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  width: 100%;
  max-width: 480px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  max-height: 90vh;
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.modal-head h3 { margin: 0; font-size: 14px; font-weight: 600; }
.modal-body { padding: 16px 18px; overflow-y: auto; }
.modal-foot {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 18px;
  border-top: 1px solid var(--border);
}
.modal-foot .spacer { flex: 1; }

.field {
  display: block;
  margin-bottom: 14px;
}
.field-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.field-label {
  display: block;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-mute);
  margin-bottom: 5px;
}
.field input[type=url],
.field input[type=number],
.field input[type=text],
.field select,
.field textarea {
  width: 100%;
  background: var(--bg-2);
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  font-family: var(--mono);
  font-size: 13px;
}
.field textarea {
  font-family: inherit;
  resize: vertical;
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none;
  border-color: var(--accent);
}
.field-hint {
  display: block;
  margin-top: 4px;
  color: var(--text-mute);
  font-size: 11.5px;
}

/* Toasts */
.toast-stack {
  position: fixed;
  right: 16px;
  bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 200;
  max-width: 320px;
}
.toast {
  background: var(--panel);
  border: 1px solid var(--border-strong);
  border-left-width: 4px;
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  box-shadow: var(--shadow);
  font-size: 13px;
  animation: slideIn 180ms ease-out;
}
.toast-triggered { border-left-color: var(--red); }
.toast-info { border-left-color: var(--accent); }
.toast-warn { border-left-color: var(--amber); }
.toast strong { display: block; margin-bottom: 2px; }
.toast-close {
  float: right;
  background: none; border: none; color: var(--text-mute);
  cursor: pointer; font-size: 16px; line-height: 1;
  margin-left: 8px;
}
@keyframes slideIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Mobile */
@media (max-width: 900px) {
  .summary-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  .summary-item.summary-meta { grid-column: 1 / -1; }
  .detail-inner { grid-template-columns: 1fr; }
}

@media (max-width: 640px) {
  body { font-size: 13px; }
  .topbar-inner { padding: 10px 14px; }
  .brand-sub { display: none; }
  .container { padding: 12px 14px 32px; }
  .summary-card { padding: 14px; }
  .summary-grid { gap: 12px; }
  .summary-value { font-size: 17px; }

  /* Filter chips compact on mobile */
  .card-head { padding: 10px 12px; }
  .filter-chips { width: 100%; overflow-x: auto; flex-wrap: nowrap; padding-bottom: 2px; }
  .chip { flex: 0 0 auto; }

  /* Sleeve head: render as plain band, not stacked card */
  .positions-table tr.sleeve-head { display: block; padding: 10px 14px 4px; background: var(--bg-2); }
  .positions-table tr.sleeve-head td { display: block; width: auto; padding: 0; border: none; white-space: normal; }
  .positions-table tr.sleeve-head .sleeve-meta { float: none; display: block; margin-top: 2px; font-size: 11.5px; }

  /* Stack the positions table into cards */
  .positions-table thead { display: none; }
  .positions-table, .positions-table tbody, .positions-table tr, .positions-table td {
    display: block;
    width: 100%;
  }
  .positions-table tr.pos-row {
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    margin: 10px 12px;
    padding: 10px 12px;
    background: var(--bg-2);
  }
  .positions-table tr.pos-row td {
    border: none;
    padding: 3px 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .positions-table tr.pos-row td.num { text-align: right; }
  .positions-table tr.pos-row td::before {
    content: attr(data-label);
    color: var(--text-mute);
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-right: 12px;
  }
  .positions-table tr.pos-row td.cell-ticker::before { display: none; }
  .positions-table tr.pos-row td.cell-ticker {
    border-bottom: 1px solid var(--border);
    padding-bottom: 8px;
    margin-bottom: 6px;
  }
  tr.detail-row {
    margin: -6px 12px 10px;
    border: 1px solid var(--border);
    border-top: none;
    border-radius: 0 0 var(--radius-sm) var(--radius-sm);
  }
  tr.detail-row td { padding: 0; }
}
