:root {
  --bg: #0d1117;
  --surface: #161b22;
  --surface2: #1c2330;
  --border: #30363d;
  --text: #e6edf3;
  --muted: #7d8590;
  --green: #3fb950;
  --green-dim: #196127;
  --red: #f85149;
  --red-dim: #6e1b19;
  --blue: #58a6ff;
  --purple: #bc8cff;
  --purple-dim: #3b2467;
  --teal: #39d0d8;
  --teal-dim: #114a4d;
  --amber: #e3b341;
  --amber-dim: #4a3a10;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
  overflow-x: hidden;
}

button, input {
  font-family: inherit;
}

#app {
  max-width: 480px;
  margin: 0 auto;
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* ---------- Header ---------- */

.header {
  flex-shrink: 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: calc(14px + var(--safe-top)) 16px 14px;
  display: flex;
  align-items: center;
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 10;
}

.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 10px;
  background: var(--surface2);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  padding: 0;
  cursor: pointer;
  color: var(--blue);
}
.icon-btn:active { background: var(--border); }

.header-titles {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex-grow: 1;
  min-width: 0;
}
.header-titles .title { font-size: 17px; font-weight: 700; letter-spacing: -0.2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.header-titles .subtitle { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.count-badge {
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px 10px;
  flex-shrink: 0;
  white-space: nowrap;
}

/* ---------- List ---------- */

.list-toolbar {
  flex-shrink: 0;
  padding: 10px 16px 0;
  display: flex;
  justify-content: flex-end;
}

.list {
  flex-grow: 1;
  overflow-y: auto;
  padding: 16px 16px calc(16px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
  -webkit-overflow-scrolling: touch;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  text-align: left;
  cursor: pointer;
  width: 100%;
}
.card:active { background: var(--surface2); }

.card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.card-ticker { font-size: 21px; font-weight: 700; letter-spacing: -0.3px; }

.pill {
  font-size: 11px;
  font-weight: 700;
  border-radius: 10px;
  padding: 3px 10px;
  flex-shrink: 0;
}
.pill.call { color: var(--green); background: var(--green-dim); }
.pill.put { color: var(--red); background: var(--red-dim); }
.pill.csp { color: var(--teal); background: var(--teal-dim); }
.pill.cc { color: var(--purple); background: var(--purple-dim); }
.pill.leaps { color: var(--amber); background: var(--amber-dim); }
.pill.stock { color: var(--blue); background: rgba(88, 166, 255, 0.14); }

.card-mid {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
}
.field-label {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.card-value { font-size: 15px; font-weight: 600; }
.card-value .dim { color: var(--muted); font-weight: 500; }
.card-value.premium { font-weight: 700; color: var(--blue); }
.col-right { display: flex; flex-direction: column; gap: 2px; align-items: flex-end; }
.col-left { display: flex; flex-direction: column; gap: 2px; }

.card-bottom {
  font-size: 12px;
  color: var(--muted);
  border-top: 1px solid var(--border);
  padding-top: 10px;
}
.card-bottom.expired { color: var(--red); }

/* ---------- Empty state ---------- */

.empty-state {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 40px 32px;
  text-align: center;
  color: var(--muted);
}
.empty-state svg { opacity: 0.5; margin-bottom: 6px; }
.empty-state .empty-title { font-size: 15px; font-weight: 600; color: var(--text); }
.empty-state .empty-sub { font-size: 13px; line-height: 1.4; }

/* ---------- Sheet ---------- */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 20;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}
.overlay.open { opacity: 1; pointer-events: auto; }

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  margin: 0 auto;
  max-width: 480px;
  max-height: 92vh;
  background: var(--bg);
  border-radius: 16px 16px 0 0;
  border: 1px solid var(--border);
  border-bottom: none;
  z-index: 21;
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform 0.25s ease;
  overflow: hidden;
}
.sheet.open { transform: translateY(0); }

/* The form itself sits between .sheet (the flex container) and
   .sheet-body (the flex item that needs to shrink to fit and scroll)
   -- take it out of layout so .sheet-body is the direct flex child. */
#tradeForm { display: contents; }

.sheet-header {
  flex-shrink: 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.sheet-header .title { font-size: 17px; font-weight: 700; letter-spacing: -0.2px; }

.sheet-body {
  flex-grow: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.field { display: flex; flex-direction: column; gap: 8px; }

.input {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  color: var(--text);
  font-size: 16px;
  width: 100%;
  appearance: none;
  -webkit-appearance: none;
}
.input::placeholder { color: var(--muted); }
.input:focus { outline: none; border-color: var(--blue); }

.input.ticker {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 0.5px;
  text-transform: uppercase;
}

.input.debit-input { font-size: 18px; font-weight: 700; color: var(--blue); }

.segmented { display: flex; gap: 8px; }
.segmented-row2 { margin-top: 8px; }
.segment {
  flex-grow: 1;
  min-width: 0;
  text-align: center;
  padding: 11px 4px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--muted);
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
}
.segment.active.call { background: var(--green-dim); border-color: var(--green); color: var(--green); font-weight: 700; }
.segment.active.put { background: var(--red-dim); border-color: var(--red); color: var(--red); font-weight: 700; }
.segment.active.ds { background: var(--surface); border-color: var(--blue); color: var(--blue); font-weight: 700; }
.segment.active.csp { background: var(--teal-dim); border-color: var(--teal); color: var(--teal); font-weight: 700; }
.segment.active.cc { background: var(--purple-dim); border-color: var(--purple); color: var(--purple); font-weight: 700; }
.segment.active.leaps { background: var(--amber-dim); border-color: var(--amber); color: var(--amber); font-weight: 700; }
.segment.active.stock { background: var(--surface); border-color: var(--blue); color: var(--blue); font-weight: 700; }

.strike-row { display: flex; gap: 10px; }
.strike-col { flex-grow: 1; min-width: 0; display: flex; flex-direction: column; gap: 6px; }
.strike-col .sub-label { font-size: 11px; color: var(--muted); }
.width-hint { font-size: 12px; color: var(--muted); padding-top: 2px; }

.debit-prefix { display: flex; align-items: center; gap: 4px; }
.debit-prefix span.dollar { color: var(--muted); font-weight: 500; font-size: 16px; }

/* Lives inside the scrollable .sheet-body (not fixed) so it's always
   reachable by scrolling, even when the iOS keyboard covers the lower
   part of the screen. */
.sheet-footer {
  margin: 4px -16px calc(-20px + var(--safe-bottom)) -16px;
  padding: 16px 16px calc(20px + var(--safe-bottom));
  border-top: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.btn {
  text-align: center;
  padding: 14px 0;
  border-radius: 10px;
  font-weight: 700;
  font-size: 15px;
  border: none;
  cursor: pointer;
  width: 100%;
}
.btn-save { background: var(--green); color: #06240d; }
.btn-save.put { background: var(--red); color: #2b0705; }
.btn-save.csp { background: var(--teal); color: #04211f; }
.btn-save.cc { background: var(--purple); color: #221238; }
.btn-save.leaps { background: var(--amber); color: #2b2005; }
.btn-save.stock { background: var(--blue); color: #06213f; }
.btn-delete { background: transparent; color: var(--red); border: 1px solid var(--red-dim); }

/* ---------- Settings sheet ---------- */

.settings-hint {
  font-size: 13px;
  line-height: 1.5;
  color: var(--muted);
}

.btn-clear-cache {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  margin-top: 4px;
}
.btn-clear-cache:active { background: var(--border); }

.error-text {
  font-size: 12px;
  color: var(--red);
  min-height: 14px;
}

/* ---------- Export sheet ---------- */

.export-list { display: flex; flex-direction: column; gap: 8px; }

.export-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  text-align: left;
  padding: 12px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--muted);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.export-row:active { background: var(--border); }
.export-row-label { flex-grow: 1; min-width: 0; }

.export-row-count {
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 3px 9px;
  flex-shrink: 0;
}

/* The tick box: outline only until the row is selected. */
.export-check {
  width: 20px;
  height: 20px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: transparent;
}
.export-row.selected .export-check { color: inherit; }

.export-row.selected { color: var(--text); }
.export-row.selected.ds { border-color: var(--blue); background: var(--surface); }
.export-row.selected.ds .export-check { border-color: var(--blue); color: var(--blue); }
.export-row.selected.csp { border-color: var(--teal); background: var(--teal-dim); }
.export-row.selected.csp .export-check { border-color: var(--teal); color: var(--teal); }
.export-row.selected.cc { border-color: var(--purple); background: var(--purple-dim); }
.export-row.selected.cc .export-check { border-color: var(--purple); color: var(--purple); }
.export-row.selected.leaps { border-color: var(--amber); background: var(--amber-dim); }
.export-row.selected.leaps .export-check { border-color: var(--amber); color: var(--amber); }
.export-row.selected.stock { border-color: var(--blue); background: var(--surface); }
.export-row.selected.stock .export-check { border-color: var(--blue); color: var(--blue); }

.export-row:disabled { opacity: 0.4; cursor: default; }

.export-actions { display: flex; gap: 16px; padding-top: 2px; }

.link-btn {
  background: none;
  border: none;
  padding: 0;
  font-size: 13px;
  font-weight: 600;
  color: var(--blue);
  cursor: pointer;
}
.link-btn:disabled { color: var(--muted); cursor: default; }
.link-btn.danger { color: var(--red); }

.optional-tag { color: var(--muted); font-weight: 400; text-transform: none; letter-spacing: 0; }

.btn-save:disabled { background: var(--surface2); color: var(--muted); cursor: default; }

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(28px + var(--safe-bottom));
  transform: translateX(-50%);
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  padding: 10px 16px;
  border-radius: 10px;
  z-index: 30;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  max-width: calc(100% - 48px);
  text-align: center;
}
