:root {
  color-scheme: dark;
}

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

button,
input,
select,
.stock {
  touch-action: manipulation;
}

button:focus-visible,
input:focus-visible,
select:focus-visible,
.stock:focus-visible,
dialog:focus-visible {
  outline: 3px solid #f3bd59;
  outline-offset: 2px;
}

.stock[role="button"] {
  border-radius: 4px;
}

@media (max-width: 760px) {
  html {
    overflow-x: hidden;
    scroll-padding-bottom: calc(76px + env(safe-area-inset-bottom));
  }

  body {
    min-width: 0;
    overflow-x: hidden;
    padding-bottom: calc(74px + env(safe-area-inset-bottom));
    background:
      radial-gradient(circle at 90% 0, rgb(66 201 223 / 8%), transparent 220px),
      var(--bg);
  }

  body:has(dialog[open]) {
    overflow: hidden;
  }

  .shell {
    width: 100%;
    max-width: none;
    padding: max(10px, env(safe-area-inset-top)) 10px 16px;
  }

  .top {
    display: block;
    margin: 0 0 10px;
  }

  .top h1 {
    font-size: 21px;
    line-height: 1.25;
    letter-spacing: .01em;
  }

  .top p {
    margin-top: 5px;
    font-size: 12px;
    line-height: 1.45;
  }

  .fresh {
    display: block;
    width: 100%;
    margin-top: 9px;
    padding: 8px 10px;
    border-left: 3px solid var(--cyan);
    white-space: normal;
    font-size: 12px;
    line-height: 1.4;
  }

  .mode-tabs {
    position: fixed;
    z-index: 1000;
    right: 0;
    bottom: 0;
    left: 0;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 4px;
    margin: 0;
    padding: 6px 6px calc(6px + env(safe-area-inset-bottom));
    border-top: 1px solid #31495f;
    border-bottom: 0;
    background: rgb(7 16 26 / 96%);
    box-shadow: 0 -10px 30px rgb(0 0 0 / 45%);
    backdrop-filter: blur(14px);
  }

  .mode-tab {
    min-width: 0;
    min-height: 54px;
    padding: 5px 2px 4px;
    border: 0;
    border-radius: 8px;
    font-size: 0;
    line-height: 1.1;
  }

  .mode-tab::before {
    display: block;
    margin-bottom: 3px;
    color: #a9bfd1;
    font-size: 17px;
    font-weight: 900;
  }

  .mode-tab::after {
    content: attr(data-mobile-label);
    display: block;
    overflow: hidden;
    color: inherit;
    font-size: 11px;
    font-weight: 800;
    line-height: 1.15;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .mode-tab[data-view="daily"]::before { content: "②"; }
  .mode-tab[data-view="branches"]::before { content: "①"; }
  .mode-tab[data-view="weekday"]::before { content: "③"; }
  .mode-tab[data-view="regime"]::before { content: "④"; }

  .mode-tab.active {
    border: 0;
    color: var(--cyan);
    background: #12283a;
    box-shadow: inset 0 3px 0 var(--cyan);
  }

  .mode-tab.active::before {
    color: var(--cyan);
  }

  .panel,
  .card,
  .step,
  .method-card,
  .metric {
    border-radius: 10px;
  }

  .panel {
    padding: 10px;
  }

  .controls,
  .catalog-controls,
  .weekday-controls {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 9px;
  }

  .controls .field,
  .catalog-controls .field:first-child,
  .catalog-controls .field:last-of-type,
  .weekday-controls .field:first-child,
  .weekday-controls .field:nth-child(2),
  .weekday-controls .field:last-of-type {
    grid-column: 1 / -1;
  }

  .regime-controls,
  .flow,
  .method-grid,
  .detail-grid,
  .weekday-overall {
    grid-template-columns: minmax(0, 1fr);
  }

  #view-branches.active {
    display: flex;
    flex-direction: column;
  }

  #view-branches > .panel {
    order: 1;
  }

  #view-branches > .flow {
    order: 2;
    margin-top: 12px;
  }

  #view-branches > .method-grid {
    order: 3;
    margin-top: 10px;
  }

  .field label {
    margin-bottom: 5px;
    font-size: 12px;
  }

  .field input,
  .field select,
  .btn {
    width: 100%;
    min-height: 46px;
    height: auto;
    border-radius: 8px;
    font-size: 16px;
  }

  .field input,
  .field select {
    padding: 0 11px;
  }

  .btn {
    padding: 10px 12px;
  }

  .metrics,
  .weekday-summary,
  .regime-summary {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  .metrics .metric:last-child:nth-child(odd),
  .weekday-summary .metric:last-child:nth-child(odd),
  .regime-summary .metric:last-child:nth-child(odd) {
    grid-column: 1 / -1;
  }

  .metric {
    min-width: 0;
    padding: 9px 10px;
  }

  .metric strong {
    font-size: 19px;
  }

  .status-line {
    display: grid;
    gap: 5px;
    padding: 8px 10px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #091725;
  }

  .notice,
  .formula,
  .bias {
    overflow-wrap: anywhere;
  }

  .notice {
    border-radius: 8px;
  }

  .group {
    margin-top: 12px;
  }

  .group-head {
    padding: 9px 10px;
    border-radius: 7px 7px 0 0;
  }

  .table-wrap {
    overflow: visible;
    border: 0;
    background: transparent;
  }

  table.mobile-card-table {
    display: block;
    width: 100%;
    white-space: normal;
  }

  table.mobile-card-table thead {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0 0 0 0);
    clip-path: inset(50%);
    white-space: nowrap;
  }

  table.mobile-card-table tbody {
    display: grid;
    gap: 10px;
    width: 100%;
  }

  table.mobile-card-table tbody tr {
    position: relative;
    display: grid;
    width: 100%;
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: linear-gradient(145deg, #102235, #0b1927);
    box-shadow: 0 5px 18px rgb(0 0 0 / 16%);
  }

  table.mobile-card-table td {
    display: grid;
    grid-template-columns: minmax(98px, .85fr) minmax(0, 1.35fr);
    gap: 10px;
    align-items: start;
    min-width: 0;
    min-height: 44px;
    padding: 9px 11px;
    border-bottom: 1px solid #1c3043;
    text-align: left !important;
    white-space: normal;
    overflow-wrap: anywhere;
  }

  table.mobile-card-table td:last-child {
    border-bottom: 0;
  }

  table.mobile-card-table td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 11px;
    font-weight: 700;
    line-height: 1.35;
  }

  table.mobile-card-table td[colspan],
  table.mobile-card-table td.empty {
    display: block;
    min-height: 76px;
    padding: 24px 12px !important;
    text-align: center !important;
  }

  table.mobile-card-table td[colspan]::before,
  table.mobile-card-table td.empty::before {
    display: none;
  }

  #daily-groups table.mobile-card-table tbody tr:not(:has(.empty)) {
    padding-top: 58px;
  }

  #daily-groups table.mobile-card-table td:nth-child(1):not(.empty) {
    position: absolute;
    z-index: 2;
    top: 10px;
    right: 10px;
    display: block;
    min-height: 30px;
    padding: 5px 9px;
    border: 1px solid #35526b;
    border-radius: 999px;
    background: #172d41;
    color: var(--text);
    font-size: 12px;
  }

  #daily-groups table.mobile-card-table td:nth-child(1)::before {
    content: "排名 ";
    display: inline;
    color: var(--muted);
  }

  #daily-groups table.mobile-card-table td:nth-child(2) {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    display: block;
    min-height: 58px;
    padding: 7px 80px 7px 11px;
    border-bottom: 1px solid #284158;
    background: #13283a;
  }

  #daily-groups table.mobile-card-table td:nth-child(2)::before {
    display: none;
  }

  #daily-groups table.mobile-card-table td:nth-child(4) {
    display: block;
  }

  #daily-groups table.mobile-card-table td:nth-child(4)::before {
    display: block;
    margin-bottom: 5px;
  }

  .stock[role="button"] {
    display: inline-flex;
    align-items: center;
    min-height: 44px;
    padding: 2px 1px;
  }

  .leaders,
  .reason {
    min-width: 0;
    max-width: none;
    width: 100%;
  }

  .leader {
    padding: 5px 0 7px;
  }

  .spark,
  .spark-empty {
    max-width: 100%;
  }

  .weekday-chart {
    display: block;
    width: 100%;
    min-width: 0;
    max-width: 100%;
    height: auto;
  }

  .weekday-rate-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .weekday-rate-grid span:last-child:nth-child(odd) {
    grid-column: 1 / -1;
  }

  .weekday-overall .card {
    min-width: 0;
    overflow: hidden;
    padding: 10px;
  }

  .legend {
    gap: 9px 13px;
    font-size: 12px;
  }

  .catalog-summary {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .pill {
    min-width: 0;
    border-radius: 7px;
    text-align: center;
  }

  .formula {
    padding: 10px;
    font-size: 12px;
  }

  dialog {
    inset: 0;
    width: 100vw;
    max-width: none;
    height: 100dvh;
    max-height: none;
    margin: 0;
    border: 0;
    border-radius: 0;
  }

  .modal-head {
    gap: 8px;
    align-items: center;
    padding: max(10px, env(safe-area-inset-top)) 10px 10px;
  }

  .modal-head h2 {
    font-size: 19px;
  }

  .close {
    flex: 0 0 46px;
    width: 46px;
    min-height: 46px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #172d41;
  }

  .modal-body {
    padding: 8px 8px calc(12px + env(safe-area-inset-bottom));
  }

  .detail-grid {
    gap: 9px;
  }

  .card {
    min-width: 0;
    padding: 9px;
  }

  .chart-title {
    gap: 8px;
    align-items: flex-start;
    font-size: 12px;
  }

  .price-chart {
    height: 310px;
  }

  .volume-chart {
    height: 105px;
  }

  .branch-list {
    max-height: none;
    overflow: visible;
  }

  .branch-detail-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  .branch-detail-grid span {
    min-width: 0;
    overflow-wrap: anywhere;
  }
}

@media (max-width: 390px) {
  .shell {
    padding-right: 8px;
    padding-left: 8px;
  }

  .panel {
    padding: 8px;
  }

  table.mobile-card-table td {
    grid-template-columns: 92px minmax(0, 1fr);
    padding-right: 9px;
    padding-left: 9px;
  }
}

@media (max-width: 760px) and (orientation: landscape) {
  body {
    padding-bottom: calc(62px + env(safe-area-inset-bottom));
  }

  .mode-tab {
    min-height: 45px;
  }

  .mode-tab::before {
    display: none;
  }

  .price-chart {
    height: 260px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: .01ms !important;
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
  }
}
