:root {
      --ink: #16202d;
      --sub: #546170;
      --line: rgba(22, 32, 45, 0.12);
      --card: rgba(255, 251, 245, 0.92);
      --card-strong: #fffdfa;
      --accent: #b25733;
      --blue: #245e9d;
      --shadow: 0 24px 60px rgba(58, 43, 26, 0.12);
      --radius: 24px;
    }

    * {
      box-sizing: border-box;
    }

    body {
      margin: 0;
      color: var(--ink);
      font-family: "Source Han Sans SC", "Noto Sans SC", "PingFang SC", "Microsoft YaHei", sans-serif;
      background:
        radial-gradient(circle at top right, rgba(210, 171, 92, 0.18), transparent 24%),
        radial-gradient(circle at left top, rgba(36, 94, 157, 0.12), transparent 20%),
        linear-gradient(180deg, #f8f2e8 0%, #f4ecdf 100%);
    }

    .shell {
      width: min(1680px, calc(100vw - 32px));
      margin: 0 auto;
      padding: 20px 0 28px;
    }

    .topbar {
      display: flex;
      align-items: center;
      justify-content: flex-start;
      padding: 12px 18px;
      border-radius: 999px;
      border: 1px solid rgba(255, 255, 255, 0.6);
      background: rgba(255, 249, 240, 0.76);
      backdrop-filter: blur(10px);
      box-shadow: 0 12px 32px rgba(58, 43, 26, 0.08);
    }

    .topbar a {
      text-decoration: none;
      color: var(--accent);
      font-size: 16px;
      font-weight: 800;
    }

    .panel {
      margin-top: 14px;
      padding: 20px 24px;
      border-radius: var(--radius);
      border: 1px solid rgba(255, 255, 255, 0.55);
      background: var(--card);
      box-shadow: var(--shadow);
      backdrop-filter: blur(10px);
    }

    .hero {
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 14px 0 2px;
    }

    .hero h1 {
      margin: 0;
      font-size: clamp(32px, 4vw, 48px);
      letter-spacing: 0.08em;
    }

    .controls-grid,
    .formula-grid,
    .content-grid,
    .summary-grid {
      display: grid;
      gap: 16px;
    }

    .settings-panel {
      display: flex;
      align-items: end;
      gap: 14px;
      padding-top: 14px;
      padding-bottom: 14px;
    }

    .controls-grid {
      grid-template-columns: minmax(180px, 1fr) minmax(180px, 1fr) 128px;
      align-items: end;
      gap: 12px;
    }

    .ratio-grid {
      grid-template-columns: 152px 152px 112px minmax(260px, 1fr);
    }

    .draw-grid {
      flex: 0 0 220px;
      grid-template-columns: minmax(190px, 1fr);
      margin-top: 0;
    }

    .control-mode {
      flex: 1 1 auto;
      min-width: 0;
    }

    .mode-tabs {
      display: inline-flex;
      gap: 6px;
      flex: 0 0 auto;
      margin-bottom: 0;
      padding: 5px;
      border: 1px solid var(--line);
      border-radius: 16px;
      background: rgba(255, 253, 250, 0.7);
    }

    .mode-tab {
      min-width: 88px;
      height: 38px;
      padding: 0 12px;
      border: none;
      border-radius: 12px;
      background: transparent;
      color: var(--sub);
      font-size: 15px;
      font-weight: 900;
      cursor: pointer;
    }

    .mode-tab.active {
      background: var(--accent);
      color: #fff;
      box-shadow: 0 10px 22px rgba(178, 87, 51, 0.22);
    }

    .control-mode[hidden] {
      display: none;
    }

    .field label {
      display: flex;
      justify-content: space-between;
      align-items: center;
      gap: 12px;
      margin-bottom: 6px;
      color: var(--ink);
      font-size: 15px;
      font-weight: 800;
    }

    .field .value {
      min-width: 40px;
      text-align: right;
      color: var(--accent);
      font-size: 18px;
    }

    .inline-field {
      display: flex;
      align-items: center;
      gap: 8px;
    }

    .inline-field label {
      flex: 0 0 auto;
      margin-bottom: 0;
      white-space: nowrap;
    }

    .inline-field input[type="number"] {
      min-width: 66px;
    }

    input[type="range"] {
      width: 100%;
      accent-color: var(--accent);
      cursor: pointer;
    }

    input[type="number"] {
      width: 100%;
      height: 40px;
      padding: 0 10px;
      border: 1px solid var(--line);
      border-radius: 14px;
      background: #fffdfa;
      color: var(--ink);
      font: 800 17px "Source Han Sans SC", "Noto Sans SC", "PingFang SC", "Microsoft YaHei", sans-serif;
    }

    input[type="number"]:focus {
      outline: 3px solid rgba(178, 87, 51, 0.16);
      border-color: rgba(178, 87, 51, 0.5);
    }

    .computed-counts {
      display: flex;
      align-items: center;
      gap: 10px;
      height: 56px;
    }

    .count-box {
      display: flex;
      align-items: center;
      justify-content: space-between;
      flex: 1;
      min-width: 118px;
      height: 56px;
      padding: 0 14px;
      border: 2px solid rgba(178, 87, 51, 0.34);
      border-radius: 16px;
      background: linear-gradient(180deg, #fff9f1 0%, #ffe9d3 100%);
      color: var(--ink);
      box-shadow: 0 10px 20px rgba(178, 87, 51, 0.14), inset 0 0 0 1px rgba(255, 255, 255, 0.65);
    }

    .count-box span {
      color: var(--sub);
      font-size: 15px;
      font-weight: 800;
    }

    .count-box strong {
      color: var(--accent);
      font-size: 34px;
      font-weight: 900;
      line-height: 1;
    }

    .scale-btn {
      height: 40px;
      border: none;
      border-radius: 18px;
      background: linear-gradient(135deg, #b25733 0%, #d48251 100%);
      color: #fff;
      font-size: 17px;
      font-weight: 800;
      cursor: pointer;
      box-shadow: 0 14px 28px rgba(178, 87, 51, 0.24);
    }

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

    .formula-card,
    .table-card,
    .chart-card,
    .summary-card {
      min-width: 0;
      padding: 18px 20px;
      border-radius: 20px;
      border: 1px solid var(--line);
      background: var(--card-strong);
    }

    .formula-title,
    .table-card h2,
    .chart-card h2,
    .summary-card h2 {
      margin: 0;
      font-size: 24px;
      font-weight: 900;
      color: var(--ink);
    }

    .formula.hyper {
      background: #fbf1ea;
    }

    .formula.binom {
      background: #edf4fb;
    }

    .formula {
      margin-top: 14px;
      padding: 16px;
      border-radius: 18px;
      overflow-x: auto;
    }

    math {
      font-size: 26px;
    }

    .content-grid {
      grid-template-columns: minmax(480px, 0.9fr) minmax(620px, 1.1fr);
      align-items: stretch;
    }

    .table-card,
    .chart-card {
      display: flex;
      flex-direction: column;
    }

    .table-title-row {
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: 12px;
    }

    .table-wrap {
      margin-top: 14px;
      overflow: auto;
      border: 1px solid var(--line);
      border-radius: 18px;
    }

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

    thead th {
      position: sticky;
      top: 0;
      z-index: 1;
      padding: 14px 12px;
      background: #f8f1e8;
      border-bottom: 1px solid var(--line);
      font-size: 20px;
      font-weight: 900;
      color: #111827;
      text-align: center;
      line-height: 1.35;
      overflow-wrap: anywhere;
    }

    thead th:nth-child(1) { width: 22%; }
    thead th:nth-child(2) { width: 27%; }
    thead th:nth-child(3) { width: 25%; }
    thead th:nth-child(4) { width: 26%; }

    tbody td {
      padding: 13px 10px;
      border-top: 1px solid rgba(22, 32, 45, 0.08);
      font-size: 20px;
      font-weight: 700;
      text-align: center;
      color: #18212e;
    }

    tbody tr:nth-child(even) td {
      background: rgba(248, 241, 232, 0.52);
    }

    tbody tr.max-gap-row td {
      background: #fff1e6;
      color: #111827;
      box-shadow: inset 0 1px 0 rgba(178, 87, 51, 0.22), inset 0 -1px 0 rgba(178, 87, 51, 0.22);
    }

    .gap-cell {
      min-width: 0;
    }

    .gap-value {
      color: var(--accent);
      font-size: 20px;
      font-weight: 900;
    }

    .chart-shell {
      margin-top: 14px;
      padding: 14px;
      border-radius: 18px;
      border: 1px solid var(--line);
      background: #fffdfa;
      flex: 1;
    }

    canvas {
      display: block;
      width: 100%;
      height: 460px;
    }

    .legend {
      display: flex;
      flex-wrap: wrap;
      gap: 18px;
      margin-top: 12px;
      font-size: 18px;
      font-weight: 800;
      color: var(--ink);
    }

    .legend-item {
      display: inline-flex;
      align-items: center;
      gap: 8px;
    }

    .legend-swatch {
      width: 16px;
      height: 16px;
      border-radius: 4px;
    }

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

    .summary-card {
      text-align: center;
      padding-top: 16px;
      padding-bottom: 16px;
    }

    .summary-value {
      margin-top: 10px;
      font-size: clamp(34px, 4vw, 44px);
      font-weight: 900;
      line-height: 1.1;
    }

    @media (max-width: 1200px) {
      .settings-panel {
        display: grid;
        align-items: stretch;
      }

      .controls-grid,
      .ratio-grid,
      .formula-grid,
      .content-grid,
      .summary-grid {
        grid-template-columns: 1fr;
      }

      canvas {
        height: 380px;
      }
    }

    @media (max-width: 720px) {
      .shell {
        width: min(100vw - 16px, 1680px);
        padding-top: 10px;
      }

      .topbar,
      .panel {
        padding-left: 16px;
        padding-right: 16px;
      }

      .hero h1,
      .formula-title,
      .table-card h2,
      .chart-card h2,
      .summary-card h2 {
        font-size: 22px;
      }

      thead th,
      tbody td,
      .legend {
        font-size: 16px;
      }

      .gap-value {
        font-size: 16px;
      }

      math {
        font-size: 22px;
      }

      canvas {
        height: 320px;
      }
    }
