:root {
  --bg: #0b1016;
  --panel: #121826;
  --text: #e6eefb;
  --muted: #9bb0c7;
  --primary: #2d72d9;
  --danger: #e55353;
  --success: #2ecc71;
  --warning: #f4c542;
  --tile-green: #103b28;
  --tile-red: #3b1010;
  --tile-neutral: #1a2333;
  --border: #243247;
  /* Controls translucency of tile backgrounds (0=transparent, 1=solid) */
  --tile-opacity: 0.38;
  --tile-opacity-hover: 0.50;
  --glass-blur: 12px;
  --glass-sat: 140%;
  /* Range indicator palette */
  --range-track: #233046;
  --range-fill-day: #4aa3ff;   /* matches primary family */
  --range-fill-w52: #7ec9ff;   /* lighter, used on hover */
  /* State-aware fills */
  --range-fill-gain: var(--success);
  --range-fill-loss: var(--danger);
  --range-fill-flat: #7a8aa9;   /* gray */
}

/* Ensure hidden attribute actually hides, even with our display rules */
[hidden] { display: none !important; }
.modal[hidden] { display: none !important; }

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; font-family: system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, 'Helvetica Neue', Arial, 'Noto Sans', 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';
  background: var(--bg); color: var(--text);
}

.app { display: grid; grid-template-columns: 280px 1fr; grid-template-rows: 64px 1fr; height: 100vh; transition: grid-template-columns 240ms ease; }
.app.sidebar-collapsed { grid-template-columns: 0px 1fr; }
.app-header { grid-column: 1 / -1; display: flex; align-items: center; justify-content: space-between; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--border); }
.app-header h1 { font-size: 18px; margin: 0 16px 0 0; }
.app-header .left { display: flex; align-items: center; gap: 12px; }
.app-header .right { display: flex; align-items: center; gap: 12px; }
.connection { font-size: 13px; color: var(--muted); }
.connection.connected { color: var(--success); }
.connection.warning { color: var(--warning); }
.connection.error { color: var(--danger); }
.muted { color: var(--muted); font-size: 12px; }

.sidebar { background: var(--panel); border-right: 1px solid var(--border); padding: 12px; overflow-y: auto; transition: opacity 200ms ease, transform 200ms ease; }
.app.sidebar-collapsed .sidebar { opacity: 0; transform: translateX(-16px); pointer-events: none; }
.sidebar-header { position: sticky; top: 0; background: var(--panel); padding: 8px 4px 8px 4px; margin: -12px -12px 8px -12px; border-bottom: 1px solid var(--border); display: flex; justify-content: flex-end; }
.icon-btn { width: 30px; height: 30px; display: grid; place-items: center; border-radius: 8px; background: #1a2340; color: var(--text); border: 1px solid var(--border); cursor: pointer; }
.icon-btn:hover { background: #202b4d; }
.icon-btn:active { transform: translateY(1px); }
/* Header-friendly ghost variant */
.icon-btn.ghost { background: transparent; border-color: var(--border); }
.icon-btn.ghost:hover { background: rgba(255,255,255,.05); }
.icon-btn svg { fill: var(--muted); }
.icon-btn:hover svg { fill: var(--text); }

/* Floating handle visible when sidebar is collapsed */
.sidebar-handle { position: fixed; top: 76px; left: 8px; z-index: 60; box-shadow: 0 2px 6px rgba(0,0,0,.35); }
@media (max-width: 900px) {
  .sidebar-handle { top: 76px; left: 8px; }
}

/* Avoid handle overlapping content titles when collapsed */
.app.sidebar-collapsed .content { padding-left: 44px; }
.card { background: #0f1420; border: 1px solid var(--border); border-radius: 8px; padding: 12px; margin-bottom: 12px; }
.legend { font-size: 12px; color: var(--muted); }
.legend-box { display: inline-block; width: 16px; height: 10px; border-radius: 2px; margin: 0 6px 0 12px; }
.legend-box.gain { background: #1f7a4a; }
.legend-box.loss { background: #7a1f1f; }
.legend-box.neutral { background: #2a3957; }

.content { padding: 12px; overflow: auto; transition: padding-left 240ms ease; }
.heatmap { display: grid; grid-template-columns: repeat(6, 1fr); gap: 12px; }
.sector { grid-column: 1 / -1; }
.sector-title { margin: 12px 0 8px 0; color: var(--muted); font-size: 14px; }
.tiles { display: grid; grid-template-columns: repeat(6, 1fr); gap: 12px; grid-auto-flow: dense; }
.tile { background: transparent; border: 1px solid rgba(255,255,255,.10); border-radius: 12px; padding: 12px; padding-bottom: 64px; min-height: 150px; position: relative; cursor: pointer; overflow: hidden; will-change: transform; transform-origin: center; backface-visibility: hidden;
  /* Explicit base transform so hover animates smoothly in all browsers */
  transform: translateY(0) scale(1) translateZ(0);
  transition: transform .22s cubic-bezier(.2,.6,.2,1), padding .15s ease, box-shadow .22s ease;
}
.tile::before { content: ''; position: absolute; inset: 0; border-radius: inherit; opacity: var(--tile-opacity); backdrop-filter: blur(var(--glass-blur)) saturate(var(--glass-sat)); -webkit-backdrop-filter: blur(var(--glass-blur)) saturate(var(--glass-sat)); z-index: 0; background: linear-gradient(135deg, rgba(255,255,255,.14), rgba(255,255,255,.06)); box-shadow: inset 0 0 0 1px rgba(255,255,255,.06), 0 6px 20px rgba(0,0,0,.25); transition: opacity .18s ease, transform .18s ease; }
.tile::after { content: ''; position: absolute; inset: 0; border-radius: inherit; z-index: 0; pointer-events: none; opacity: var(--tile-tint-opacity, 0.28); }
.tile.gain::after { background: var(--tile-green); }
.tile.loss::after { background: var(--tile-red); }
.tile.flat::after { background: var(--tile-neutral); }
.tile > * { position: relative; z-index: 1; }
.tiles .tile:hover { transform: translateY(-2px) scale(1.04); z-index: 10; padding-bottom: 64px; box-shadow: 0 8px 26px rgba(0,0,0,.35); }
.tiles .tile:hover::before { transform: scale(1.01); }
.tile:hover::before { opacity: var(--tile-opacity-hover); }

@supports not ((backdrop-filter: blur(1px))) {
  .tile::before { background: rgba(20,28,44,.55); opacity: 1; }
}
.tile .sym { font-weight: 600; }
.tile .name { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile .price { font-size: 14px; }
.tile .change { font-size: 12px; }
.tile .meta { position: absolute; left: auto; right: 12px; bottom: 56px; font-size: 11px; color: var(--muted); opacity: 0; transition: opacity .15s ease; pointer-events: none; z-index: 2; text-align: right; max-width: 50%; line-height: 1.2; }
.tile:hover .meta { opacity: 1; }
.tile .levels { font-size: 11px; color: var(--muted); margin-top: 6px; }
.tile .levels { display: none !important; }
.tile .prev { position: absolute; left: 12px; right: 12px; bottom: 64px; font-size: 11px; color: var(--muted); z-index: 2; pointer-events: none; }
/* color tint handled via ::after */
.progress { position: absolute; left: 8px; right: 8px; bottom: 8px; height: 6px; background: var(--range-track); border-radius: 999px; overflow: hidden; box-shadow: inset 0 1px 0 rgba(255,255,255,.06); z-index: 1; }
.progress > span { display: block; height: 100%; background: var(--range-fill-day); }
.progress.day > span { background: var(--range-fill-day); }
.progress.w52 > span { background: var(--range-fill-w52); }

/* Pin indicator above progress bar */
.progress .pin { position: absolute; top: -14px; width: 0; height: 0; left: 0; transform: translateX(-50%); }
.progress.day .pin::before { content: ''; display: block; width: 8px; height: 8px; background: var(--range-fill-day); border-radius: 50%; margin: 0 auto; box-shadow: 0 1px 2px rgba(0,0,0,.35); }
.progress.day .pin::after { content: ''; position: absolute; top: 7px; left: 50%; transform: translateX(-50%); border-left: 5px solid transparent; border-right: 5px solid transparent; border-top: 6px solid var(--range-fill-day); }
/* Match the daily style for 52-week bar */
.progress.w52 .pin::before { content: ''; display: block; width: 8px; height: 8px; background: var(--range-fill-w52); border-radius: 50%; margin: 0 auto; box-shadow: 0 1px 2px rgba(0,0,0,.35); }
.progress.w52 .pin::after { content: ''; position: absolute; top: 7px; left: 50%; transform: translateX(-50%); border-left: 5px solid transparent; border-right: 5px solid transparent; border-top: 6px solid var(--range-fill-w52); }

/* State-aware progress bar colors (gain/flat/loss) */
.tile.gain .progress.day > span,
.tile.gain .progress.w52 > span { background: var(--range-fill-gain) !important; }
.tile.loss .progress.day > span,
.tile.loss .progress.w52 > span { background: var(--range-fill-loss) !important; }
.tile.flat .progress.day > span,
.tile.flat .progress.w52 > span { background: var(--range-fill-flat) !important; }

/* State-aware pin colors */
.tile.gain .progress .pin::before { background: var(--range-fill-gain) !important; }
.tile.gain .progress .pin::after { border-top-color: var(--range-fill-gain) !important; }
.tile.loss .progress .pin::before { background: var(--range-fill-loss) !important; }
.tile.loss .progress .pin::after { border-top-color: var(--range-fill-loss) !important; }
.tile.flat .progress .pin::before { background: var(--range-fill-flat) !important; }
.tile.flat .progress .pin::after { border-top-color: var(--range-fill-flat) !important; }

/* 52-week indicator bar */
.progress.w52 { bottom: 28px; height: 6px; opacity: .95; }
.progress.w52::before { content: '52w'; position: absolute; left: 6px; bottom: 6px; font-size: 10px; color: var(--muted); }
/* Daily high/low indicator label */
.progress.day::before { content: 'Day'; position: absolute; left: 6px; bottom: 6px; font-size: 10px; color: var(--muted); }

/* Labels for low/high near bars */
.bar-labels { position: absolute; left: 8px; right: 8px; display: flex; justify-content: space-between; font-size: 10px; color: var(--muted); pointer-events: none; z-index: 3; }
.bar-labels.day { bottom: 18px; }
.bar-labels.w52 { bottom: 40px; }

/* Hide 52-week indicator until hover to keep tiles compact */
.tile .progress.w52,
.tile .bar-labels.w52 { opacity: 0; pointer-events: none; transform: translateY(4px); transition: opacity .15s ease, transform .15s ease; }
.tile:hover .progress.w52,
.tile:hover .bar-labels.w52 { opacity: 1; transform: translateY(0); }
/* Move 52-week indicator up slightly on hover */
.tile:hover .progress.w52 { bottom: calc(28px + 2%); }
.tile:hover .bar-labels.w52 { bottom: calc(40px + 2%); }
/* Slightly lower the Day bar on hover to create tiny top margin */
.tile:hover .progress.day { bottom: 6px; }

.tile-actions { position: absolute; top: 6px; right: 6px; display: flex; gap: 6px; z-index: 5; padding: 4px; margin: -4px; }
.tile-actions .icon { background: rgba(0,0,0,.2); color: #cfe2ff; border: 1px solid rgba(255,255,255,.15); border-radius: 6px; width: 28px; height: 28px; line-height: 24px; font-size: 16px; text-align: center; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; }
.tile-actions .icon:hover { background: rgba(255,255,255,.08); }

/* Keep actions always interactive; soften visibility when not hovered */
.tile .tile-actions { opacity: .85; transform: translateY(0); pointer-events: auto; transition: opacity .15s ease; }
.tile:hover .tile-actions { opacity: 1; }
/* Larger tap targets on touch devices */
@media (hover: none) {
  .tile-actions .icon { width: 32px; height: 32px; line-height: 28px; font-size: 18px; }
}

/* Drag & drop highlights */
.tiles.drop-target { outline: 2px dashed #2a3a52; outline-offset: 4px; border-radius: 8px; }
.tile.dragging { opacity: .7; }

.sector-title { display: flex; align-items: center; gap: 8px; }
.sector-title .mini-btn { font-size: 11px; padding: 3px 6px; border-radius: 4px; background: #1a2340; color: var(--muted); border: 1px solid var(--border); cursor: pointer; }
.sector-title .mini-btn:hover { color: #e6eefb; }

.btn { background: #1a2340; color: #dfe8f7; border: 1px solid var(--border); border-radius: 6px; padding: 8px 12px; cursor: pointer; }
.btn:hover { background: #1f2a4f; }
.btn:disabled { opacity: .6; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: #2a63b7; }
.btn-primary:hover { background: #2b65bf; }

.input { width: 100%; background: #0e1523; color: var(--text); border: 1px solid var(--border); border-radius: 6px; padding: 8px; }
.stack { display: grid; gap: 8px; }

.search-results { position: relative; max-height: 220px; overflow: auto; border: 1px solid var(--border); border-radius: 6px; margin-top: 6px; background: #0e1523; }
.search-results .item { padding: 8px; border-bottom: 1px solid #172033; cursor: pointer; }
.search-results .item:hover { background: #16203a; }
.search-results.small { max-height: 160px; }

.spinner { width: 16px; height: 16px; border: 2px solid #7a8aa9; border-top-color: #e6eefb; border-radius: 50%; display: inline-block; animation: spin 1s linear infinite; vertical-align: middle; }
.spinner.small { width: 14px; height: 14px; border-width: 2px; }
@keyframes spin { from { transform: rotate(0deg) } to { transform: rotate(360deg) } }

.skeleton-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 12px; }
.skeleton-tile { height: 150px; background: linear-gradient(90deg, #1a2333 25%, #22314a 37%, #1a2333 63%); background-size: 400% 100%; animation: shimmer 1.4s ease infinite; border-radius: 6px; }
@keyframes shimmer { 0% { background-position: 100% 0 } 100% { background-position: -100% 0 } }

.error { color: #ffd3d3; background: #3a1515; border: 1px solid #5f1e1e; padding: 8px 12px; border-radius: 6px; }

.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; z-index: 50; }
.modal-content { width: 420px; background: #0f1420; border: 1px solid var(--border); border-radius: 8px; padding: 16px; }
.modal-content.large { width: 800px; }
.modal-content.copilot { width: 520px; max-height: 80vh; overflow: auto; }
.modal.side { display: block; }
.modal.side .modal-content.copilot { position: fixed; right: 16px; top: 16px; bottom: 16px; height: auto; max-height: calc(100vh - 32px); border-radius: 10px; }
.modal.side .modal-content { box-shadow: 0 10px 30px rgba(0,0,0,.45); }
.form { display: grid; gap: 10px; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

.lot-row { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr 1fr auto; gap: 8px; align-items: center; border-bottom: 1px solid #172033; padding: 6px 0; }
.lot-row .actions { display: flex; gap: 6px; }

/* Copilot floating button */
.fab-bot { position: fixed; right: 16px; bottom: 16px; width: 48px; height: 48px; border-radius: 50%; background: #1a2340; border: 1px solid var(--border); color: #cfe2ff; display: grid; place-items: center; cursor: pointer; z-index: 60; box-shadow: 0 4px 14px rgba(0,0,0,.35); }
.fab-bot:hover { background: #223056; }
.copilot-body { display: grid; gap: 10px; }
.copilot-body .section { border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; background: rgba(16,22,34,.6); }
.copilot-body .section h4 { margin: 0 0 6px 0; color: var(--text); font-size: 14px; }
.copilot-body .item { font-size: 13px; color: var(--muted); }
.copilot-filters { display: grid; gap: 8px; border: 1px solid var(--border); border-radius: 8px; padding: 10px; background: rgba(16,22,34,.6); }
.copilot-filters .row { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.copilot-filters label { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.copilot-filters input[type="number"] { width: 80px; }

@media (max-width: 1200px) {
  .heatmap, .tiles { grid-template-columns: repeat(5, 1fr); }
}
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; grid-template-rows: 64px auto auto; }
  .sidebar { grid-column: 1; order: 2; }
  .content { grid-column: 1; order: 3; }
  .heatmap, .tiles { grid-template-columns: repeat(3, 1fr); }
}
