:root {
  color-scheme: light;
  --paper: #fff;
  --site-red: #cc2c2c;
  --red-text: #9a2424;
  --red-pale: #fff4f3;
  --green-text: #235e47;
  --green-pale: #edf8f1;
  --ink: #333;
  --muted: #595959;
  --line: #ddd;
  --line-strong: #bfbfbf;
  background: #edfcf6;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { max-width: 800px; min-height: 100vh; margin: 0 auto; background: var(--paper); color: var(--ink); font: 14px/1.45 "Microsoft YaHei", Helvetica, Arial, sans-serif; }
p, h1, h2, h3 { margin: 0; }
button, input, select { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; }
:focus-visible { outline: 2px solid #005a9e; outline-offset: 2px; }
main { min-width: 0; padding: 10px; }
.skip-link { position: fixed; z-index: 5; top: 7px; left: 7px; padding: 7px 12px; background: var(--paper); color: var(--red-text); transform: translateY(-160%); }
.skip-link:focus { transform: translateY(0); }

.site-header { position: relative; display: grid; place-items: center; height: 45px; background: var(--site-red); color: var(--paper); }
.site-header h1 { font-size: 18px; font-weight: 400; }
.site-header :focus-visible { outline-color: var(--paper); }
.home-link { position: absolute; left: 10px; display: inline-flex; min-height: 30px; align-items: center; gap: 4px; color: inherit; font-size: 13px; text-decoration: none; white-space: nowrap; }
.home-link span { font-size: 24px; line-height: 1; }
.home-link:hover { text-decoration: underline; }
.kjt-history-tools [aria-disabled='true'] { color: var(--muted); cursor: default; }

.draw-context { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 6px 12px; margin-bottom: 8px; }
.region-switch { display: flex; gap: 4px; }
.region-switch button { min-width: 63px; min-height: 34px; padding: 3px 12px; color: var(--ink); background: var(--paper); border: 1px solid var(--line-strong); border-radius: 3px; white-space: nowrap; }
.region-switch button:hover { background: var(--red-pale); border-color: var(--site-red); }
.region-switch button[aria-pressed='true'] { color: var(--paper); background: var(--site-red); border-color: var(--site-red); }
.draw-summary { display: flex; flex-wrap: wrap; gap: 2px 12px; color: var(--muted); font-size: 13px; }
.draw-summary:empty { display: none; }
.draw-summary strong { color: var(--ink); font-variant-numeric: tabular-nums; }
.summary-total { padding-left: 12px; border-left: 1px solid var(--line); white-space: nowrap; }
.year-link-notice { margin-bottom: 8px; color: var(--red-text); font-size: 13px; }

.workspace-nav { display: flex; justify-content: space-between; align-items: center; gap: 5px; margin-bottom: 8px; border-bottom: 1px solid var(--line); }
.tabs { display: flex; min-width: 0; gap: clamp(4px, 2vw, 16px); }
.tabs button { min-height: 36px; padding: 5px 6px; border: 0; border-bottom: 2px solid transparent; background: transparent; color: var(--muted); white-space: nowrap; }
.tabs button[aria-selected='true'] { color: var(--red-text); border-color: var(--site-red); font-weight: 700; }
.tabs button:hover { color: var(--red-text); }
.button { min-height: 32px; padding: 4px 9px; border: 1px solid transparent; border-radius: 3px; background: transparent; color: var(--ink); white-space: nowrap; }
.button.primary { color: var(--paper); background: var(--site-red); border-color: var(--site-red); }
.button.primary:hover { background: var(--red-text); border-color: var(--red-text); }
.button.secondary { background: var(--paper); border-color: var(--line-strong); }
.button.secondary:hover, .button.subtle:hover { background: #f2f2f2; }
.button.subtle, .button.link-button { color: var(--red-text); }
.button.link-button { padding-inline: 5px; }
.button.link-button:hover { text-decoration: underline; text-underline-offset: 2px; }
.button:disabled { opacity: .55; }
.refresh-button { font-size: 13px; }
.refresh-button span { line-height: 1; }

.panel { min-width: 0; background: var(--paper); border: 1px solid var(--line); }
.section-heading { display: flex; flex-wrap: wrap; align-items: start; justify-content: space-between; gap: 5px 12px; }
.section-heading h2, .load-error h2 { font-size: 16px; line-height: 1.3; }
.section-heading p { margin-top: 2px; color: var(--muted); font-size: 13px; }
.section-heading.compact { align-items: center; }
.load-error { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 8px 12px; margin-bottom: 8px; padding: 10px 12px; border-color: #e6abab; background: var(--red-pale); }
.load-error p { margin-top: 4px; color: var(--red-text); }
.loading { padding: 18px 10px; text-align: center; color: var(--muted); }
.scope-panel { margin-bottom: 8px; padding: 6px 10px 10px; }
.scope-form { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)) minmax(68px, .65fr); gap: 8px; margin-top: 5px; }
.field { display: grid; align-content: start; gap: 3px; min-width: 0; color: var(--ink); font-size: 13px; }
.field input, .field select, .page-size select { width: 100%; min-width: 0; min-height: 34px; padding: 4px 6px; color: var(--ink); background: var(--paper); border: 1px solid var(--line-strong); border-radius: 3px; font-size: 14px; }
.field input::placeholder { color: #666; }
.field input[aria-invalid='true'] { border-color: var(--site-red); background: var(--red-pale); }
.field-error { margin-top: 6px; color: var(--red-text); font-weight: 600; font-size: 13px; }
.number-error { margin: 0 0 8px; overflow-wrap: anywhere; }

.filter-details { margin-bottom: 8px; }
.filter-details summary { padding: 8px 10px; cursor: pointer; color: var(--ink); background: #f3f3f3; font-weight: 700; }
.filter-details summary > span { margin-left: 4px; }
.filter-count { margin-left: 5px; color: var(--red-text); font-size: 12px; font-variant-numeric: tabular-nums; }
.filter-details[open] summary { border-bottom: 1px solid var(--line); }
.filter-details-body { padding: 7px 10px 10px; }
.number-inputs { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin: 8px 0 10px; }
.attribute-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 9px 12px; }
.attribute-group { min-width: 0; margin: 0; padding: 0; border: 0; }
.attribute-group.wide { grid-column: 1 / -1; }
.attribute-group legend { margin-bottom: 4px; padding: 0; font-size: 13px; font-weight: 700; }
.attribute-options { display: flex; flex-wrap: wrap; gap: 4px; }
.attribute-options button { display: inline-flex; gap: 4px; align-items: center; justify-content: center; min-width: 33px; min-height: 33px; padding: 3px 6px; border: 1px solid var(--line-strong); border-radius: 3px; color: var(--ink); background: var(--paper); }
.attribute-options button:hover { border-color: var(--green-text); }
.attribute-options button[aria-pressed='true'] { color: var(--green-text); background: var(--green-pale); border-color: var(--green-text); font-weight: 700; }
#history-attributes .attribute-options button[aria-pressed='true'] { color: #fff; background: var(--site-red); border-color: var(--site-red); box-shadow: inset 0 0 0 1px #ffffff40; }
#history-attributes .attribute-options button[aria-pressed='true'] .ball-dot { box-shadow: 0 0 0 1px #fff; }
.attribute-options[data-mode='exclude'] button[aria-pressed='true'] { color: var(--red-text); background: var(--red-pale); border-color: var(--red-text); }
.attribute-options button[data-opposite='true'] { text-decoration: underline dotted; text-underline-offset: 4px; }
.opposite-selection { margin-top: 3px; color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }

.results-heading { align-items: center; padding: 8px 10px; }
.page-size { display: inline-flex; flex: 0 0 auto; align-items: center; gap: 4px; color: var(--muted); font-size: 13px; white-space: nowrap; }
.page-size select { width: auto; min-height: 32px; }
.table-scroll { width: 100%; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.history-table { min-width: 416px; }
th, td { padding: 5px 10px; text-align: left; white-space: nowrap; }
thead th { background: #eee; color: var(--ink); border-block: 1px solid var(--line); font-size: 13px; }
tbody th, tbody td { border-bottom: 1px solid #eee; }
tbody tr:nth-child(even) { background: #fafafa; }
tbody tr:hover { background: var(--green-pale); }
.history-table th[scope='row'] { color: var(--red-text); }
.history-date { color: var(--muted); }
.history-balls { display: flex; gap: clamp(4px, 1.4vw, 12px); }
.special-cell { background: #fffaf3; }
.special-head { color: var(--red-text); }
.ball-cell { display: inline-grid; justify-items: center; gap: 1px; }
.ball { display: inline-grid; width: 32px; height: 32px; place-items: center; padding: 0; border: 0; border-radius: 50%; background: var(--ball-art, #eee) center / contain no-repeat; color: #222; font-size: 14px; font-weight: 700; line-height: 1; }
.ball-red { --ball-art: url('/static/2/images/ball-red.png'); --ball-color: #b52231; }
.ball-blue { --ball-art: url('/static/2/images/ball-blue.png'); --ball-color: #17619e; }
.ball-green { --ball-art: url('/static/2/images/ball-green.png'); --ball-color: #297440; }
.ball-zodiac { color: var(--muted); font-size: 12px; line-height: 1.2; }
.history-table .ball-cell.marked { position: relative; border-radius: 4px; background: #fff0b3; box-shadow: 0 0 0 2px #d8a125, 0 2px 5px #8d650126; }
.history-table .ball-cell.marked .ball-zodiac { color: #754500; font-weight: 700; }
.match-check { position: absolute; top: -3px; right: -3px; z-index: 1; display: grid; place-items: center; width: 13px; height: 13px; border: 1px solid #fff; border-radius: 50%; color: #fff; background: #b92828; font: 700 10px/1 Arial, sans-serif; }
.match-description { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.match-legend { display: flex; align-items: center; gap: 5px; margin-top: 4px; color: #754500; font-size: 12px; }
.match-swatch { display: inline-grid; place-items: center; width: 15px; height: 15px; border: 1px solid #d8a125; border-radius: 3px; color: #a82424; background: #fff0b3; font: 700 12px/1 Arial, sans-serif; }
.ball-dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--ball-color, #666); }
.empty-state { padding: 16px 10px; color: var(--muted); overflow-wrap: anywhere; }
.empty-state.is-error { color: var(--red-text); }
.pagination { display: flex; align-items: center; justify-content: center; gap: 8px; padding: 7px; color: var(--muted); font-size: 13px; }
.pagination:empty { display: none; }
.pagination .page-label { min-width: 68px; text-align: center; font-variant-numeric: tabular-nums; }

.candidate-layout, .rank-layout { display: grid; grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr); gap: 8px; align-items: start; }
.rank-layout { grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr); }
.candidate-filters, .candidate-results, .distribution { padding: 8px 10px 10px; }
.candidate-results { position: sticky; top: 10px; }
.condition-mode { display: flex; flex-wrap: wrap; gap: 4px; margin: 9px 0; }
.condition-mode button { min-height: 33px; padding: 3px 8px; border-radius: 3px; border: 1px solid var(--line-strong); color: var(--muted); background: var(--paper); font-size: 13px; }
.condition-mode button:hover, .condition-mode button[aria-pressed='true'] { color: var(--green-text); background: var(--green-pale); border-color: var(--green-text); }
.condition-mode button[data-mode='exclude'][aria-pressed='true'] { color: var(--red-text); background: var(--red-pale); border-color: var(--red-text); }
.candidate-inputs { margin-bottom: 5px; }
.fine-print { color: var(--muted); font-size: 12px; }
.catalog-year { margin-top: 3px; color: var(--muted); font-size: 13px; }
.catalog-year strong { color: var(--ink); }
.action-bar { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 9px; }
.action-bar .button { font-size: 13px; }
.action-message { margin-top: 6px; color: var(--green-text); font-size: 13px; overflow-wrap: anywhere; }
.action-message.is-error { color: var(--red-text); }
.candidate-score { display: flex; gap: 4px; align-items: baseline; margin-top: 6px; color: var(--muted); font-variant-numeric: tabular-nums; }
.candidate-score strong { color: var(--red-text); font-size: 22px; line-height: 1.15; }
.pool-balls { display: grid; grid-template-columns: repeat(auto-fit, minmax(33px, 1fr)); gap: 6px 4px; list-style: none; margin: 7px 0 0; padding: 8px 0 0; border-top: 1px solid var(--line); }
.pool-balls:empty { display: none; }
.pool-balls li { text-align: center; }
.candidate-empty { margin-top: 8px; padding: 8px; background: #f3f3f3; }
.copy-fallback { margin-top: 8px; }

.rank-note { margin: 0 10px 8px; color: var(--muted); font-size: 13px; }
.rank-controls { display: flex; flex-wrap: wrap; align-items: center; gap: 5px 10px; }
.rank-table { min-width: 368px; }
.rank-table [scope='col']:not(:first-child), .rank-table td { text-align: right; }
.rank-ball { text-decoration: underline; text-underline-offset: 2px; }
.rank-ball:hover { outline: 1px solid var(--green-text); outline-offset: 1px; }
.rank-attribute { min-width: 32px; padding-inline: 5px; color: var(--red-text); text-decoration: underline; text-underline-offset: 2px; }
.distribution-group { margin-top: 8px; padding-top: 7px; border-top: 1px solid var(--line); }
.distribution-group h3 { margin-bottom: 5px; font-size: 14px; }
.distribution-rows { display: grid; gap: 3px; margin: 0; padding: 0; list-style: none; }
.distribution-row { display: grid; grid-template-columns: 34px minmax(18px, 1fr) auto; align-items: center; gap: 7px; color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.distribution-row > span:first-child { display: inline-flex; gap: 4px; align-items: center; white-space: nowrap; }
.distribution-row meter { appearance: none; width: 100%; min-width: 0; height: 6px; margin: 0; }
.distribution-row meter::-webkit-meter-bar { height: 6px; background: #edf0ed; border: 0; }
.distribution-row meter::-webkit-meter-optimum-value { background: #3e8961; }
.distribution-row meter::-moz-meter-bar { background: #3e8961; }
.distribution-value { min-width: 77px; text-align: right; }

@media (max-width: 700px) {
  .scope-form { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .rank-layout { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 590px) {
  .scope-form { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .field input, .field select { font-size: 16px; }
  .candidate-layout { grid-template-columns: minmax(0, 1fr); }
  .candidate-results { position: static; grid-row: 1; }
  .pool-balls:not(:empty) { max-height: 160px; overflow-y: auto; overscroll-behavior: contain; }
}

@media (max-width: 430px) {
  .tabs button { padding-inline: 3px; }
  .refresh-button { padding-inline: 4px; }
  .history-table { min-width: 350px; }
  th, td { padding-inline: 5px; }
  .history-table .ball { width: 25px; height: 25px; font-size: 12px; }
}
