:root {
  --ink: #171717;
  --muted: #67645f;
  --paper: #f6f2e9;
  --panel: #fffcf4;
  --line: #d9d1c2;
  --red: #b3332f;
  --green: #1c7f5a;
  --blue: #245a89;
  --gold: #b88922;
  --charcoal: #26231f;
  --control-height: 42px;
  --control-width: 180px;
  color-scheme: light;
}

* {
  box-sizing: border-box;
}

html {
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background:
    linear-gradient(90deg, rgba(38, 35, 31, 0.05) 1px, transparent 1px) 0 0 / 28px 28px,
    linear-gradient(0deg, rgba(38, 35, 31, 0.04) 1px, transparent 1px) 0 0 / 28px 28px,
    var(--paper);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  font-family: "Avenir Next", "Noto Sans TC", "PingFang TC", sans-serif;
}

button,
input,
select {
  font: inherit;
}

.app-header {
  flex: 0 0 auto;
  padding: 28px clamp(16px, 4vw, 44px) 18px;
  border-bottom: 2px solid var(--charcoal);
  background: linear-gradient(135deg, #fffaf0 0%, #f4ead9 58%, #dfe9e1 100%);
}

.header-inner {
  width: min(100%, 1480px);
  margin: 0 auto;
  display: grid;
  grid-template-columns: minmax(310px, max-content) minmax(0, 1fr) 270px;
  gap: 12px;
  align-items: end;
}

.brand-block {
  grid-column: 1;
  grid-row: 1;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--blue);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: clamp(30px, 4vw, 48px);
  line-height: 0.95;
  letter-spacing: 0;
}

.summary-strip {
  grid-column: 3;
  grid-row: 1;
  display: grid;
  grid-template-columns: repeat(3, minmax(86px, 1fr));
  align-self: stretch;
  border: 2px solid var(--charcoal);
  background: var(--panel);
  box-shadow: 7px 7px 0 var(--charcoal);
}

.summary-item {
  padding: 12px 14px;
  border-left: 1px solid var(--line);
}

.summary-item:first-child {
  border-left: 0;
}

.summary-item strong {
  display: block;
  font-size: 24px;
}

.summary-item span {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.layout {
  width: min(100%, 1480px);
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  margin: 0 auto;
  padding: 18px clamp(12px, 3vw, 34px) 34px;
  overflow: hidden;
}

.toolbar {
  grid-column: 2;
  grid-row: 1;
  flex: 0 0 auto;
  display: flex;
  gap: 10px;
  align-items: end;
  flex-wrap: wrap;
  justify-content: flex-start;
  margin-bottom: 0;
}

.tabs {
  display: inline-grid;
  grid-template-columns: 1fr 1fr;
  width: var(--control-width);
  height: var(--control-height);
  border: 2px solid var(--charcoal);
  background: var(--panel);
}

.tab {
  min-height: 0;
  height: 100%;
  padding: 0 8px;
  border: 0;
  border-right: 1px solid var(--charcoal);
  background: transparent;
  cursor: pointer;
  font-weight: 800;
  font-size: 14px;
  line-height: 1.1;
  white-space: nowrap;
}

.tab:last-child {
  border-right: 0;
}

.tab.is-active {
  background: var(--charcoal);
  color: #fffaf0;
}

label {
  position: relative;
  display: block;
  width: var(--control-width);
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.toolbar label > span {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

select,
input {
  width: var(--control-width);
  height: var(--control-height);
  min-height: var(--control-height);
  border: 2px solid var(--charcoal);
  border-radius: 0;
  background: var(--panel);
  color: var(--ink);
  font-size: 14px;
  padding: 0 12px;
}

input[type="search"] {
  min-width: 0;
}

.content-grid {
  display: block;
  flex: 1 1 auto;
  min-height: 0;
}

.table-shell {
  width: 100%;
  max-width: 100%;
  height: 100%;
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  border: 2px solid var(--charcoal);
  background: rgba(255, 252, 244, 0.94);
  box-shadow: 7px 7px 0 rgba(38, 35, 31, 0.18);
}

.table-headline {
  flex: 0 0 auto;
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
  padding: 16px;
  border-bottom: 2px solid var(--charcoal);
}

.table-headline h2 {
  font-size: 22px;
}

.table-headline p,
#rowCount {
  color: var(--muted);
  font-size: 13px;
}

.table-wrap {
  width: 100%;
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  max-height: none;
}

table {
  width: 100%;
  min-width: 1320px;
  border-collapse: collapse;
  table-layout: fixed;
}

th,
td {
  padding: 11px 12px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
  font-size: 13px;
}

th {
  position: sticky;
  top: 0;
  z-index: 1;
  height: 44px;
  padding-top: 0;
  padding-bottom: 0;
  background: #efe3d0;
  color: var(--charcoal);
  font-size: 12px;
  line-height: 1.25;
  white-space: nowrap;
  text-transform: uppercase;
  vertical-align: middle;
}

th[data-key="name"],
td[data-key="name"] {
  width: 150px;
  white-space: nowrap;
}

th[data-key="code"],
td[data-key="code"] {
  width: 82px;
}

th[data-key="market"],
td[data-key="market"] {
  width: 82px;
}

th[data-key="period"],
td[data-key="period"] {
  width: 136px;
}

th[data-key="days_remaining"],
td[data-key="days_remaining"],
th[data-key="release_date"],
td[data-key="release_date"] {
  width: 96px;
}

th[data-key="status"],
td[data-key="status"] {
  width: 96px;
}

th[data-key="base_close"],
td[data-key="base_close"],
th[data-key="current_close"],
td[data-key="current_close"] {
  width: 102px;
}

th[data-key="return_pct"],
td[data-key="return_pct"] {
  width: 98px;
}

th[data-key="ma20"],
td[data-key="ma20"] {
  width: 84px;
}

th[data-key="ma20_slope_pct"],
td[data-key="ma20_slope_pct"] {
  width: 116px;
}

th[data-key="price_above_ma20"],
td[data-key="price_above_ma20"] {
  width: 104px;
}

.stock-link {
  color: var(--ink);
  font-weight: 800;
  text-decoration: none;
  white-space: nowrap;
}

.stock-link:hover {
  color: var(--blue);
  text-decoration: underline;
  text-underline-offset: 3px;
}

.period-cell {
  display: inline-grid;
  gap: 2px;
  line-height: 1.35;
  white-space: nowrap;
}

.sort-button {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  width: max-content;
  min-height: 44px;
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  cursor: pointer;
  font-size: inherit;
  font-weight: 900;
  line-height: inherit;
  white-space: nowrap;
}

.sort-button:hover {
  color: var(--blue);
}

.sort-indicator {
  display: inline-block;
  min-width: 10px;
  color: var(--blue);
  font-weight: 900;
  line-height: 1;
  transform: translateY(-1px);
}

tbody tr {
  cursor: pointer;
}

tbody tr:hover,
tbody tr.is-selected {
  background: #e8f0e8;
}

.expanded-row {
  cursor: default;
  background: #d7e9f5;
}

.expanded-row td {
  padding: 0;
  border-bottom: 2px solid var(--charcoal);
  width: auto;
}

.expanded-row:hover {
  background: #d7e9f5;
}

.code {
  font-weight: 900;
  color: var(--blue);
}

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 0 8px;
  border: 1px solid var(--charcoal);
  background: #fff;
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

.positive {
  color: var(--red);
  font-weight: 900;
}

.negative {
  color: var(--green);
  font-weight: 900;
}

.neutral {
  color: var(--muted);
  font-weight: 800;
}

.expanded-detail {
  padding: 10px 12px 12px;
  border-top: 2px solid var(--blue);
}

.detail-copy {
  display: grid;
  grid-template-columns: 0.7fr 1.3fr;
  gap: 10px;
}

.detail-section {
  min-width: 0;
  overflow: hidden;
  padding: 9px 10px;
  border: 1px solid var(--charcoal);
  background: rgba(255, 252, 244, 0.72);
}

.detail-section h4 {
  margin: 0 0 4px;
  font-size: 12px;
  color: var(--blue);
}

.detail-section p {
  color: var(--charcoal);
  font-size: 12px;
  line-height: 1.45;
  white-space: pre-line;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.hidden {
  display: none;
}

@media (max-width: 980px) {
  .header-inner {
    grid-template-columns: 1fr;
    align-items: start;
    gap: 12px;
  }

  .brand-block,
  .summary-strip,
  .toolbar {
    grid-column: 1;
    grid-row: auto;
  }

  .summary-strip {
    width: 100%;
  }

}

@media (max-width: 960px) {
  :root {
    --control-height: 38px;
  }

  .app-header {
    padding: 12px 10px 10px;
  }

  h1 {
    font-size: clamp(28px, 8vw, 38px);
  }

  .layout {
    padding: 10px 8px 8px;
  }

  .toolbar {
    display: grid;
    grid-template-columns: minmax(0, 0.95fr) minmax(0, 1.05fr);
    gap: 8px;
    width: 100%;
  }

  .tabs {
    grid-column: 1 / -1;
    width: 100%;
  }

  input[type="search"] {
    width: 100%;
    min-width: 0;
  }

  #releasedFilterWrap.hidden + #searchInput {
    grid-column: 1 / -1;
  }

  #releasedFilterWrap {
    min-width: 0;
    width: 100%;
  }

  select {
    width: 100%;
  }

  .summary-strip {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    box-shadow: 5px 5px 0 var(--charcoal);
  }

  .summary-item {
    min-width: 0;
    padding: 8px 10px;
    border-top: 0;
    border-left: 1px solid var(--line);
  }

  .summary-item strong {
    font-size: 20px;
    line-height: 1.05;
  }

  .summary-item span {
    font-size: 11px;
  }

  .summary-item:first-child {
    border-left: 0;
  }

  .tab,
  select,
  input {
    min-height: 38px;
  }

  .tabs {
    min-height: 38px;
  }

  .table-wrap {
    max-height: none;
    overflow: auto;
  }

  table,
  thead,
  tbody,
  tr,
  td {
    display: block;
    width: 100%;
  }

  table {
    min-width: 0;
  }

  thead {
    display: none;
  }

  tbody {
    display: grid;
    gap: 10px;
    padding: 10px;
  }

  th,
  td {
    border-bottom: 0;
  }

  .stock-row {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px 12px;
    padding: 12px;
    border: 1px solid var(--line);
    background: rgba(255, 252, 244, 0.72);
  }

  .stock-row.is-selected {
    border-color: var(--blue);
    background: #e8f0e8;
  }

  .stock-row:hover {
    background: #e8f0e8;
  }

  .stock-row td {
    min-width: 0;
    width: 100%;
    padding: 0;
    font-size: 15px;
  }

  .stock-row td::before {
    content: attr(data-label);
    display: block;
    margin-bottom: 3px;
    color: var(--muted);
    font-size: 11px;
    font-weight: 800;
  }

  .stock-row td[data-key="code"] {
    grid-column: 1;
    grid-row: 1;
    align-self: start;
  }

  .stock-row td[data-key="code"] .code {
    font-size: 24px;
  }

  .stock-row td[data-key="name"] {
    grid-column: 2;
    grid-row: 1;
  }

  .stock-row td[data-key="market"] {
    grid-column: 1;
    grid-row: 2;
  }

  .stock-row td[data-key="status"] {
    grid-column: 2;
    grid-row: 2;
  }

  .stock-row td[data-key="period"] {
    grid-column: 1;
    grid-row: 3;
  }

  .stock-row td[data-key="days_remaining"],
  .stock-row td[data-key="release_date"] {
    grid-column: 2;
    grid-row: 3;
  }

  .stock-row td[data-key="base_close"] {
    grid-column: 1;
    grid-row: 4;
  }

  .stock-row td[data-key="current_close"] {
    grid-column: 2;
    grid-row: 4;
  }

  .stock-row td[data-key="return_pct"] {
    grid-column: 1;
    grid-row: 5;
  }

  .stock-row td[data-key="ma20"] {
    grid-column: 2;
    grid-row: 5;
  }

  .stock-row td[data-key="ma20_slope_pct"] {
    grid-column: 1;
    grid-row: 6;
  }

  .stock-row td[data-key="price_above_ma20"] {
    grid-column: 2;
    grid-row: 6;
  }

  .expanded-row {
    display: block;
    width: 100%;
  }

  .expanded-row td {
    display: block;
    width: 100%;
  }

  .expanded-detail {
    padding: 10px;
  }

  .table-shell {
    box-shadow: 4px 4px 0 rgba(38, 35, 31, 0.16);
  }
}

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

@media (max-width: 440px) {
  .summary-strip {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .summary-item {
    border-top: 0;
    border-left: 1px solid var(--line);
  }

  .summary-item:first-child {
    border-left: 0;
  }
}
