:root {
  color-scheme: light dark;
  --bg: #f6f7f9;
  --card: #ffffff;
  --tx: #1b1f24;
  --mut: #6b7280;
  --bd: #d9dde3;
  --pri: #1f3864;
  --g: #0a7a3d;
  --r: #c0262d;
  --radius: 8px;
  --space: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c;
    --card: #1d2127;
    --tx: #e8eaed;
    --mut: #9aa3ae;
    --bd: #333a44;
    --pri: #6f93d6;
    --g: #4cc57f;
    --r: #ff7b80;
  }
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0 auto;
  font: 16px/1.5 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--tx);
  padding: 16px;
  max-width: 1200px;
  min-height: 100vh;
}

h1 {
  font-size: 1.375rem; /* 22px */
  margin: 0 0 16px;
  font-weight: 700;
  line-height: 1.3;
}

h2 {
  font-size: 1rem; /* 16px */
  margin: 0 0 12px;
  color: var(--pri);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

/* Card Container */
.card {
  background: var(--card);
  border: 1px solid var(--bd);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}

/* Card Action Button Bar (Prevents cutoff / overflow) */
.card > p {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin: 16px 0 0 0;
}

/* Form Layout Grid */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 12px;
}

/* Handle inline style column spans cleanly */
.grid > div[style*="grid-column:span 2"],
.grid > div[style*="grid-column: span 2"] {
  grid-column: span 2;
}

.grid > div[style*="grid-column:1/-1"],
.grid > div[style*="grid-column: 1/-1"],
.grid > div[style*="grid-column: 1 / -1"] {
  grid-column: 1 / -1;
}

label {
  display: block;
  font-size: 0.75rem; /* 12px */
  color: var(--mut);
  margin-bottom: 4px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

input,
select,
textarea {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--bd);
  border-radius: 6px;
  background: var(--bg);
  color: var(--tx);
  font: inherit;
  font-size: 1rem; /* 16px to prevent iOS auto-zoom */
  min-height: 42px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--pri);
  box-shadow: 0 0 0 2px rgba(31, 56, 100, 0.15);
}

textarea {
  min-height: 80px;
  resize: vertical;
}

/* Buttons */
button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 8px 16px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: var(--pri);
  color: #ffffff;
  font-weight: 600;
  font-size: 0.875rem; /* 14px */
  cursor: pointer;
  min-height: 42px;
  touch-action: manipulation;
  transition: opacity 0.15s ease, background-color 0.15s ease;
}

button:hover {
  opacity: 0.92;
}

button.sec {
  background: transparent;
  color: var(--pri);
  border: 1px solid var(--bd);
}

button.sec:hover {
  background: rgba(128, 128, 128, 0.08);
  border-color: var(--pri);
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* KPI Summary Cards */
.kpis {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}

.k {
  background: var(--bg);
  border: 1px solid var(--bd);
  border-radius: 6px;
  padding: 12px 10px;
  text-align: center;
}

.k b {
  display: block;
  font-size: 1.25rem; /* 20px */
  font-weight: 700;
  line-height: 1.2;
  font-variant-numeric: tabular-nums;
}

.k span {
  font-size: 0.75rem; /* 12px */
  color: var(--mut);
  margin-top: 4px;
  display: block;
  font-weight: 500;
}

/* Two-column layout */
.two {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 16px;
}

/* Scrollable Table Wrapper */
.w {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

table {
  border-collapse: collapse;
  width: 100%;
  font-size: 0.875rem; /* 14px */
}

th,
td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--bd);
  text-align: left;
  white-space: nowrap;
}

th {
  color: var(--mut);
  position: sticky;
  top: 0;
  background: var(--card);
  font-weight: 600;
  font-size: 0.75rem; /* 12px */
  text-transform: uppercase;
  letter-spacing: 0.04em;
  z-index: 1;
}

tbody tr:hover {
  background-color: rgba(128, 128, 128, 0.04);
}

/* Main Trade Table (#tb) Numerical Alignment */
#tb th:nth-child(4), #tb td:nth-child(4), /* MC */
#tb th:nth-child(5), #tb td:nth-child(5), /* SL */
#tb th:nth-child(6), #tb td:nth-child(6), /* TP */
#tb th:nth-child(7), #tb td:nth-child(7), /* RRR */
#tb th:nth-child(8), #tb td:nth-child(8), /* PnL */
#tb th:nth-child(9), #tb td:nth-child(9), /* Result */
#tb th:nth-child(10), #tb td:nth-child(10), /* R */
#tb th:nth-child(11), #tb td:nth-child(11), /* Overshoot */
#tb th:nth-child(12), #tb td:nth-child(12)  /* Hold */ {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* Summary Tables (#t1, #t2) Column Alignment */
#t1 th:nth-child(n+2), #t1 td:nth-child(n+2),
#t2 th:nth-child(n+2), #t2 td:nth-child(n+2) {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* Colors for Wins / Losses */
.g, i.g {
  color: var(--g);
  font-style: normal;
}

.r, i.r {
  color: var(--r);
  font-style: normal;
}

#st {
  font-size: 0.875rem;
  color: var(--mut);
  margin-left: 8px;
  font-weight: 400;
}

/* Action Links in Table */
td a {
  color: var(--pri);
  text-decoration: none;
  font-size: 1.125rem;
  margin-right: 6px;
  padding: 2px 6px;
  border-radius: 4px;
  display: inline-block;
}

td a:hover {
  background: rgba(128, 128, 128, 0.1);
}

th.srt {
  user-select: none;
  cursor: pointer;
}

th.srt:hover {
  color: var(--tx);
}

/* Mobile & Small Screens */
@media (max-width: 640px) {
  body {
    padding: 12px;
  }

  h1 {
    font-size: 1.25rem;
  }

  .card {
    padding: 12px;
    margin-bottom: 12px;
  }

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

  .grid > div[style*="grid-column:span 2"],
  .grid > div[style*="grid-column: span 2"] {
    grid-column: 1 / -1;
  }

  .kpis {
    grid-template-columns: repeat(2, 1fr);
  }

  .two {
    grid-template-columns: 1fr;
  }

  table {
    font-size: 0.8125rem; /* 13px */
  }

  th,
  td {
    padding: 8px 10px;
  }

  button {
    flex: 1 1 auto;
    min-width: 120px;
  }
}

@media (max-width: 420px) {
  .grid {
    grid-template-columns: 1fr;
  }

  .grid > div {
    grid-column: 1 / -1 !important;
  }

  .kpis {
    grid-template-columns: 1fr 1fr;
  }
}
