:root {
  color-scheme: light;
  --ink: #102b54;
  --ink-soft: #52677e;
  --teal: #087f8c;
  --teal-deep: #056975;
  --teal-soft: #e9f7f7;
  --cyan-line: #a9d7df;
  --paper: #ffffff;
  --page: #f5fafb;
  --sand: #fff8eb;
  --sand-line: #e7c981;
  --coral: #f05b2d;
  --coral-soft: #fff0e8;
  --plum: #7257d7;
  --green: #13934d;
  --danger: #bd3d2e;
  --line: #d4e0e7;
  --line-strong: #9fc5d0;
  --shadow: 0 8px 28px rgba(28, 66, 85, 0.08);
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Noto Sans SC", "Microsoft YaHei", sans-serif;
  --math: "Cambria Math", "STIX Two Math", "Times New Roman", serif;
}

* { box-sizing: border-box; }

html { min-width: 320px; background: var(--page); }

body {
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(rgba(27, 123, 142, 0.025) 1px, transparent 1px),
    linear-gradient(90deg, rgba(27, 123, 142, 0.025) 1px, transparent 1px),
    var(--page);
  background-size: 28px 28px;
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; }
button, a, input, select, textarea { -webkit-tap-highlight-color: transparent; }
button { color: inherit; }

button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 3px solid rgba(0, 145, 163, 0.26);
  outline-offset: 2px;
}

.app-shell { min-height: 100vh; }

.topbar {
  height: 80px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 0 clamp(22px, 3vw, 46px);
  border-bottom: 2px solid var(--teal);
  background: rgba(255, 255, 255, 0.97);
}

.back-link,
.brand {
  display: inline-flex;
  align-items: center;
  color: var(--ink);
  text-decoration: none;
}

.back-link { justify-self: start; gap: 10px; font-size: 17px; font-weight: 720; }
.back-link svg { width: 25px; height: 25px; fill: none; stroke: var(--teal); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.back-link:hover { color: var(--teal-deep); }

.brand { gap: 13px; }
.brand h1 { margin: 0; font-family: var(--math); font-size: 35px; line-height: 1; letter-spacing: 0.04em; }
.brand-mark { display: block; width: 58px; height: 46px; object-fit: contain; }

.privacy-note { justify-self: end; display: inline-flex; align-items: center; gap: 9px; color: #0a6881; font-size: 15px; font-weight: 680; }
.privacy-note svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.operation-ribbon {
  display: grid;
  grid-template-columns: 1.35fr 1.1fr 0.78fr 0.76fr;
  border-bottom: 1px solid var(--line-strong);
  background: var(--paper);
  box-shadow: 0 4px 18px rgba(30, 74, 94, 0.05);
}

.operation-group { min-width: 0; padding: 9px 12px 12px; border-right: 1px solid var(--line); }
.operation-group:last-child { border-right: 0; }
.operation-group h2 { margin: 0 0 7px 4px; color: var(--teal-deep); font-size: 13px; font-weight: 800; letter-spacing: 0.04em; }
.decomposition-group h2, .decomposition-group .op-symbol { color: var(--plum); }
.operation-list { display: flex; min-width: 0; gap: 3px; }

.operation-button {
  min-width: 56px;
  min-height: 58px;
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 5px 4px;
  border: 1px solid transparent;
  border-radius: 5px;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  font-size: 11px;
  line-height: 1.12;
  white-space: nowrap;
}
.operation-button:hover { border-color: var(--cyan-line); background: #f3fbfc; }
.operation-button.is-selected { color: #fff; border-color: var(--teal-deep); background: var(--teal); }
.operation-button.is-selected .op-symbol { color: #fff; }
.op-symbol { color: #096f91; font-family: var(--math); font-size: 20px; line-height: 1; }

main { padding: 0; }

.workspace {
  min-height: 642px;
  display: grid;
  grid-template-columns: minmax(460px, 1fr) minmax(560px, 1.08fr);
  border-bottom: 2px solid var(--teal);
  background: rgba(255, 255, 255, 0.68);
}

.input-panel,
.result-panel {
  position: relative;
  min-width: 0;
  padding: 18px clamp(22px, 2.6vw, 40px) 24px;
  background: rgba(255,255,255,0.86);
}
.input-panel { border-right: 2px solid var(--teal); overflow: hidden; }
.result-panel { background: rgba(255,255,255,0.93); }

.section-title-row { min-height: 38px; display: flex; align-items: center; justify-content: space-between; gap: 20px; }
.section-title-row h2 { margin: 0; color: var(--teal-deep); font-size: 24px; line-height: 1.1; letter-spacing: 0.02em; }
.precision-control { display: flex; align-items: center; gap: 8px; color: var(--ink-soft); font-size: 13px; }
.precision-control select { height: 34px; min-width: 62px; padding: 0 8px; border: 1px solid var(--line); border-radius: 5px; background: #fff; color: var(--ink); }
.current-operation { color: var(--teal); font-family: var(--math); font-size: 16px; }

.input-mode-tabs { display: grid; grid-template-columns: repeat(3, 1fr); width: min(340px, 72%); margin: 12px 0 16px; border: 1px solid var(--line-strong); border-radius: 5px; overflow: hidden; }
.input-mode-tabs button { height: 38px; border: 0; border-right: 1px solid var(--line); background: #f9fbfc; color: var(--ink); cursor: pointer; font-size: 14px; }
.input-mode-tabs button:last-child { border-right: 0; }
.input-mode-tabs button[aria-selected="true"] { color: var(--teal-deep); background: var(--teal-soft); box-shadow: inset 0 -3px 0 var(--teal); font-weight: 760; }

.matrix-editor { position: relative; margin-top: 3px; padding: 0 48px 18px 0; border-bottom: 1px dashed var(--cyan-line); }
.matrix-editor + .matrix-editor { margin-top: 17px; }
.matrix-editor-header { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 10px; }
.matrix-editor h3 { margin: 0; font-size: 17px; }
.dimension-controls { display: flex; gap: 12px; }
.dimension-controls label { display: inline-flex; align-items: center; gap: 6px; color: var(--ink-soft); font-size: 13px; }
.dimension-controls input { width: 62px; height: 34px; padding: 0 6px; border: 1px solid var(--line); border-radius: 5px; background: #fff; color: var(--ink); text-align: center; }

.matrix-scroll { max-width: 100%; overflow: auto; scrollbar-color: var(--cyan-line) transparent; }
.matrix-grid {
  --columns: 3;
  display: grid;
  grid-template-columns: 38px repeat(var(--columns), minmax(74px, 1fr));
  width: 100%;
  min-width: 100%;
  gap: 4px 6px;
  align-items: stretch;
}
.matrix-corner,
.matrix-column-label,
.matrix-row-label { display: grid; place-items: center; min-height: 30px; color: #1a5073; font-family: var(--math); font-size: 14px; }
.matrix-row-label { min-height: 39px; border: 1px solid var(--line); border-radius: 4px; background: #f5fafb; }
.matrix-cell {
  width: 100%;
  min-width: 74px;
  height: 39px;
  padding: 0 10px;
  border: 1px solid #cad9e1;
  border-radius: 3px;
  background: #fff;
  color: #10274c;
  font-family: var(--math);
  font-size: 17px;
  text-align: center;
}
.matrix-cell:focus { border-color: var(--teal); background: var(--teal-soft); outline: 0; }
.matrix-editor.has-error .matrix-cell { border-color: #d9887a; background: #fff8f6; }
.matrix-preview-note,
.matrix-output-note { margin: 7px 0 0; color: var(--ink-soft); font-size: 11px; line-height: 1.4; text-align: center; }

.matrix-clear { position: absolute; right: 4px; top: 67px; width: 38px; height: 38px; display: grid; place-items: center; border: 0; background: transparent; color: var(--teal); cursor: pointer; }
.matrix-clear:hover { color: var(--coral); background: var(--coral-soft); }
.matrix-clear svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.import-panel { min-height: 365px; padding: 20px; border: 1px solid var(--line); border-radius: 6px; background: rgba(248,252,252,0.78); }
.import-heading { display: flex; align-items: start; justify-content: space-between; gap: 18px; margin-bottom: 16px; }
.import-heading h3 { margin: 0 0 5px; font-size: 18px; }
.import-heading p { margin: 0; color: var(--ink-soft); font-size: 13px; line-height: 1.55; }
.import-heading label { display: flex; align-items: center; gap: 8px; color: var(--ink-soft); font-size: 13px; white-space: nowrap; }
.import-heading select { height: 34px; min-width: 58px; padding: 0 8px; border: 1px solid var(--line); border-radius: 5px; background: #fff; }
.import-panel textarea { width: 100%; min-height: 230px; resize: vertical; padding: 14px; border: 1px solid var(--line-strong); border-radius: 5px; background: #fff; color: var(--ink); font-family: ui-monospace, "SFMono-Regular", Consolas, monospace; font-size: 14px; line-height: 1.6; }
.file-drop { min-height: 230px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; border: 1px dashed var(--teal); border-radius: 6px; background: #fff; color: var(--teal-deep); cursor: pointer; text-align: center; }
.file-drop svg { width: 42px; height: 42px; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }
.file-drop span { color: var(--ink-soft); font-size: 13px; }
.secondary-action { min-height: 40px; margin-top: 14px; padding: 0 20px; border: 1px solid var(--teal); border-radius: 5px; background: #fff; color: var(--teal-deep); cursor: pointer; font-weight: 720; }
.secondary-action:hover { background: var(--teal-soft); }
.secondary-action:disabled { cursor: not-allowed; opacity: 0.5; }

.scalar-control { display: flex; align-items: center; justify-content: center; gap: 12px; margin: 16px auto 0; }
.scalar-control label { font-weight: 720; }
.scalar-control input { width: 130px; height: 40px; padding: 0 12px; border: 1px solid var(--line-strong); border-radius: 5px; background: #fff; text-align: center; }
.input-error { margin: 13px 0 0; padding: 10px 13px; border-left: 4px solid var(--danger); background: #fff2ef; color: #8d3026; font-size: 13px; line-height: 1.45; }

.calculate-button { width: min(380px, 66%); min-height: 52px; display: block; margin: 17px auto 0; border: 1px solid var(--teal-deep); border-radius: 5px; background: var(--teal); color: #fff; cursor: pointer; font-size: 19px; font-weight: 800; letter-spacing: 0.06em; box-shadow: 0 5px 12px rgba(8,127,140,0.15); }
.calculate-button:hover { background: var(--teal-deep); }
.calculate-button:disabled { cursor: wait; background: #5f8f96; box-shadow: none; }

.coastal-decoration { position: absolute; left: 8px; bottom: -4px; width: 155px; height: 78px; pointer-events: none; opacity: 0.55; }
.coastal-decoration .seaweed { fill: none; stroke: #5bbac0; stroke-width: 4; stroke-linecap: round; }
.coastal-decoration .shell { fill: #fff1e8; stroke: #df927b; stroke-width: 2; stroke-linejoin: round; }
.coastal-decoration .shore { fill: none; stroke: #d9bd83; stroke-width: 2; }

.result-empty { min-height: 460px; display: grid; place-content: center; justify-items: center; color: var(--ink-soft); text-align: center; }
.result-empty svg { width: 70px; height: 70px; fill: none; stroke: var(--cyan-line); stroke-width: 1.5; }
.output-tabs { min-height: 35px; display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 8px; }
.output-tabs:empty { display: none; }
.output-tab { min-height: 34px; padding: 0 13px; border: 1px solid var(--line); border-radius: 4px; background: #f8fbfc; color: var(--ink-soft); cursor: pointer; font-size: 13px; }
.output-tab[aria-selected="true"] { border-color: var(--sand-line); background: var(--sand); color: #85580d; font-weight: 760; }

.result-summary-grid { display: grid; grid-template-columns: minmax(260px, 1fr) minmax(230px, 0.85fr); gap: 22px; align-items: start; }
.primary-output h3,
.metrics-panel h3 { margin: 0 0 10px; font-size: 16px; }
.result-matrix-scroll { max-width: 100%; overflow: auto; }
.result-matrix { border-collapse: collapse; min-width: min(100%, 350px); margin: 0 auto; font-family: var(--math); font-size: 17px; }
.result-matrix th,
.result-matrix td { min-width: 58px; height: 40px; padding: 6px 10px; border: 1px solid #d7c58d; text-align: center; }
.result-matrix th { min-width: 42px; background: #fffdf7; color: var(--ink-soft); font-weight: 500; }
.result-matrix td { background: #fffaf0; }
.result-matrix td.is-emphasis { color: var(--coral); background: var(--coral-soft); font-weight: 800; }
.scalar-output { min-height: 130px; display: grid; place-items: center; border: 1px solid var(--sand-line); background: var(--sand); color: var(--coral); font-family: var(--math); font-size: clamp(30px, 5vw, 58px); }

.metrics-panel { border: 1px solid var(--line); border-radius: 5px; background: #fff; overflow: hidden; }
.metrics-panel h3 { padding: 10px 13px; margin: 0; border-bottom: 1px solid var(--line); background: #f8fbfc; color: var(--teal-deep); }
.metrics-panel dl { margin: 0; }
.metrics-panel dl div { display: grid; grid-template-columns: minmax(130px, 1fr) auto; gap: 12px; padding: 6px 12px; border-bottom: 1px dashed var(--line); font-size: 12.5px; }
.metrics-panel dl div:last-child { border-bottom: 0; }
.metrics-panel dt { color: var(--ink-soft); }
.metrics-panel dd { margin: 0; color: var(--ink); font-family: var(--math); text-align: right; }

.steps-panel { margin-top: 18px; border: 1px solid var(--sand-line); border-radius: 5px; background: var(--sand); overflow: hidden; }
.steps-panel h3 { margin: 0; padding: 9px 14px; border-bottom: 1px solid var(--sand-line); color: #87590f; font-size: 16px; }
.steps-panel #steps-content { min-height: 88px; padding: 11px 15px; }
.steps-panel p { margin: 0 0 7px; color: #39475d; font-size: 13px; line-height: 1.45; }
.steps-panel p:last-child { margin-bottom: 0; }
.steps-panel p.math-line { font-family: var(--math); font-size: 16px; }

.verification-panel { min-height: 45px; display: grid; grid-template-columns: auto 1fr; align-items: center; gap: 18px; margin-top: 12px; padding: 8px 14px; border: 1px solid var(--sand-line); border-radius: 5px; background: #fffaf1; }
.verification-panel h3 { margin: 0; color: #87590f; font-size: 14px; }
.verification-value { display: flex; align-items: center; justify-content: space-between; gap: 15px; color: var(--ink); font-family: var(--math); font-size: 14px; }
.verification-status { display: inline-flex; align-items: center; gap: 6px; color: var(--green); font-family: var(--sans); font-weight: 760; white-space: nowrap; }
.verification-status.is-warning { color: var(--coral); }
.verification-status::before { content: "✓"; width: 20px; height: 20px; display: grid; place-items: center; border-radius: 50%; background: currentColor; color: #fff; font-size: 12px; }
.verification-status.is-warning::before { content: "!"; }

.bottom-panel { display: grid; grid-template-columns: minmax(0, 1.7fr) minmax(300px, 1fr); background: #fff; }
.history-panel, .export-panel { min-width: 0; padding: 15px 28px 20px; }
.history-panel { border-right: 1px solid var(--line); }
.bottom-heading { min-height: 34px; display: flex; align-items: center; justify-content: space-between; gap: 15px; border-bottom: 1px solid var(--cyan-line); }
.bottom-heading h2 { display: inline-flex; align-items: center; gap: 8px; margin: 0; color: var(--teal-deep); font-size: 17px; }
.bottom-heading svg { width: 21px; height: 21px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.text-button { border: 0; background: transparent; color: var(--teal); cursor: pointer; font-size: 12px; }
.text-button:hover { color: var(--coral); }

.history-list { min-height: 92px; display: grid; grid-template-columns: repeat(3, minmax(190px, 1fr)); gap: 0; }
.history-empty { grid-column: 1 / -1; display: grid; place-items: center; color: var(--ink-soft); font-size: 13px; }
.history-item { min-width: 0; display: grid; grid-template-columns: 1fr auto; gap: 5px 10px; padding: 13px 16px; border-right: 1px dashed var(--cyan-line); }
.history-item:last-child { border-right: 0; }
.history-item time { grid-column: 1 / -1; color: var(--ink-soft); font-size: 11px; }
.history-item strong { min-width: 0; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history-item p { grid-column: 1 / -1; margin: 0; color: var(--ink-soft); font-size: 11.5px; }
.history-actions { display: flex; gap: 4px; }
.history-actions button { width: 28px; height: 28px; display: grid; place-items: center; border: 1px solid transparent; background: transparent; color: var(--teal); cursor: pointer; }
.history-actions button:hover { border-color: var(--sand-line); background: var(--sand); color: var(--coral); }
.history-actions svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.export-actions { display: grid; grid-template-columns: repeat(2, 1fr); gap: 9px; margin-top: 14px; }
.export-actions button { min-height: 40px; padding: 0 10px; border: 1px solid var(--line-strong); border-radius: 4px; background: #f8fbfc; color: var(--teal-deep); cursor: pointer; font-size: 12px; font-weight: 720; }
.export-actions button:hover { border-color: var(--teal); background: var(--teal-soft); }
.export-actions button:disabled { cursor: not-allowed; opacity: 0.45; }
.export-note { margin: 10px 0 0; color: var(--ink-soft); font-size: 11px; line-height: 1.45; }

[hidden] { display: none !important; }

@media (max-width: 1180px) {
  .operation-ribbon { display: flex; overflow-x: auto; }
  .operation-group { min-width: max-content; }
  .operation-button { min-width: 64px; }
  .workspace { grid-template-columns: minmax(410px, 0.84fr) minmax(510px, 1fr); }
  .result-summary-grid { grid-template-columns: 1fr; }
  .metrics-panel dl { display: grid; grid-template-columns: repeat(2, 1fr); }
  .metrics-panel dl div:nth-child(odd) { border-right: 1px dashed var(--line); }
}

@media (max-width: 860px) {
  .topbar { height: 70px; padding-inline: 16px; }
  .brand h1 { font-size: 27px; }
  .brand-mark { width: 47px; height: 40px; }
  .back-link span, .privacy-note span { display: none; }
  .workspace { display: block; }
  .input-panel { border-right: 0; border-bottom: 2px solid var(--teal); }
  .input-panel, .result-panel { padding-inline: 22px; }
  .result-summary-grid { grid-template-columns: minmax(250px, 1fr) minmax(220px, 0.78fr); }
  .bottom-panel { grid-template-columns: 1fr; }
  .history-panel { border-right: 0; border-bottom: 1px solid var(--line); }
}

@media (max-width: 620px) {
  .topbar { grid-template-columns: 38px 1fr 38px; }
  .brand { gap: 7px; }
  .brand h1 { font-size: 24px; }
  .brand-mark { width: 40px; }
  .privacy-note { justify-self: end; }
  .operation-group { padding-inline: 8px; }
  .operation-group h2 { font-size: 11px; }
  .operation-button { min-width: 58px; font-size: 10px; }
  .input-panel, .result-panel { padding: 17px 14px 22px; }
  .section-title-row h2 { font-size: 21px; }
  .current-operation { max-width: 46%; font-size: 13px; text-align: right; }
  .input-mode-tabs { width: 100%; }
  .matrix-editor { padding-right: 0; }
  .matrix-editor-header { align-items: flex-start; }
  .dimension-controls { gap: 7px; }
  .dimension-controls label { display: grid; grid-template-columns: auto 54px; gap: 3px; }
  .dimension-controls input { width: 54px; }
  .matrix-clear { top: 107px; right: 0; background: rgba(255,255,255,0.9); border: 1px solid var(--line); }
  .matrix-scroll { padding-right: 42px; }
  .calculate-button { width: 82%; }
  .coastal-decoration { display: none; }
  .result-summary-grid { grid-template-columns: 1fr; }
  .metrics-panel dl { display: block; }
  .metrics-panel dl div:nth-child(odd) { border-right: 0; }
  .verification-panel { grid-template-columns: 1fr; gap: 6px; }
  .verification-value { align-items: flex-start; }
  .history-panel, .export-panel { padding-inline: 15px; }
  .history-list { display: block; }
  .history-item { border-right: 0; border-bottom: 1px dashed var(--cyan-line); }
  .history-item:last-child { border-bottom: 0; }
}

@media print {
  .operation-ribbon, .input-panel, .bottom-panel, .back-link, .privacy-note { display: none !important; }
  .topbar { grid-template-columns: 1fr; border-bottom: 1px solid #777; }
  .brand { justify-self: center; }
  .workspace { display: block; border: 0; }
  .result-panel { padding: 20px; }
}
