:root {
  --bg: #f4f7fb;
  --surface: #ffffff;
  --ink: #172033;
  --muted: #5e6a7d;
  --line: #cfd8e6;
  --accent: #265bd7;
  --accent-strong: #173b91;
  --good: #14824f;
  --bad: #ba2d2d;
  --warn: #a06500;
  --shadow: 0 10px 28px rgba(26, 43, 74, 0.12);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  color: var(--ink);
  background: var(--bg);
  font-family: Arial, Helvetica, sans-serif;
  line-height: 1.45;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  min-height: 34px;
  padding: 6px 10px;
  cursor: pointer;
}

button:hover {
  border-color: var(--accent);
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 700;
}

button.primary:hover {
  background: var(--accent-strong);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.app-header {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  align-items: center;
  padding: 8px 18px;
  background: #ffffff;
  border-bottom: 1px solid var(--line);
}

.header-main {
  display: flex;
  flex-wrap: nowrap;
  gap: 14px;
  align-items: center;
  min-width: 0;
}

.app-header h1 {
  margin: 0;
  font-size: 22px;
  line-height: 1.15;
  white-space: nowrap;
}

.panel-toggles {
  display: inline-flex;
  flex-wrap: nowrap;
  gap: 6px;
  align-items: center;
  white-space: nowrap;
}

.app-header .panel-toggles label {
  display: inline-flex;
  gap: 6px;
  align-items: center;
  width: auto;
  min-height: 26px;
  padding: 3px 7px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f9fbff;
  color: var(--ink);
  font-size: 12px;
  font-weight: 700;
}

.app-header .panel-toggles input[type="checkbox"] {
  flex: 0 0 auto;
  width: 15px;
  height: 15px;
  min-height: 15px;
  padding: 0;
  margin: 0;
}

.app-header p {
  margin: 5px 0 0;
  color: var(--muted);
}

.score-card {
  min-width: 180px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f9fbff;
  display: inline-flex;
  align-items: baseline;
  gap: 10px;
  text-align: left;
}

.score-card span {
  display: inline;
  font-weight: 700;
}

.score-card span + span {
  margin-top: 0;
  color: var(--muted);
  font-size: 13px;
  font-weight: 400;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(250px, 330px) minmax(420px, 1fr) minmax(280px, 390px);
  align-items: start;
  gap: 12px;
  padding: 10px;
}

.controls-panel,
.question-panel,
.tool-panel,
.math-panel,
.model-board-wrap {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.controls-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px;
}

.controls-panel label {
  margin-bottom: 0;
}

label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

select,
input,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  min-height: 34px;
  padding: 6px 9px;
}

textarea {
  resize: vertical;
}

.question-nav,
.controls-panel .control-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}

.controls-panel .control-grid button,
.controls-panel .question-nav button {
  width: 100%;
}

.button-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.button-row {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.question-panel {
  grid-column: 2;
  grid-row: 1;
  align-self: stretch;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 16px 20px;
}

.question-meta {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.question-heading {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: flex-start;
  margin: 12px 0 14px;
}

.question-panel h2 {
  flex: 1 1 520px;
  margin: 0;
  max-width: 1120px;
  font-size: 23px;
  line-height: 1.24;
}

.video-tutorial-jump {
  display: inline-flex;
  flex: 0 0 auto;
  gap: 7px;
  align-items: center;
  min-height: 30px;
  padding: 5px 9px;
  border-color: #b9c9f2;
  background: #f7faff;
  color: var(--accent-strong);
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
}

.video-tutorial-jump::before {
  width: 0;
  height: 0;
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
  border-left: 10px solid var(--accent);
  content: "";
}

.video-tutorial-jump span {
  display: none;
}

.answer-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin-top: auto;
  font-size: 18px;
}

.answer-row input {
  width: min(330px, 100%);
  font-size: 19px;
  font-weight: 700;
}

.feedback {
  min-height: 20px;
  margin: 10px 0 0;
  font-weight: 700;
}

.feedback.good {
  color: var(--good);
}

.feedback.bad {
  color: var(--bad);
}

.feedback.warn {
  color: var(--warn);
}

.video-tutorial-card {
  display: grid;
  gap: 10px;
  max-width: 920px;
  margin: 0 auto;
  padding: 12px;
  border: 1px solid #b9c9f2;
  border-radius: 8px;
  background: #f7faff;
}

.video-tutorial-card[hidden] {
  display: none;
}

.video-tutorial-copy {
  display: grid;
  gap: 2px;
}

.video-tutorial-copy strong {
  color: var(--accent-strong);
  font-size: 15px;
}

.video-tutorial-copy span {
  color: var(--muted);
  font-size: 13px;
}

.video-frame-wrap {
  width: min(760px, 100%);
  aspect-ratio: 16 / 9;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #e8eef8;
}

.video-frame-wrap iframe {
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
}

.video-tutorial-link {
  width: fit-content;
  color: var(--accent-strong);
  font-size: 13px;
  font-weight: 700;
  text-decoration: none;
}

.video-tutorial-link:hover {
  text-decoration: underline;
}

.tutor-card,
.solver-panel {
  margin-top: 10px;
  padding: 10px;
  border: 1px solid #b9c9f2;
  border-radius: 8px;
  background: #f7faff;
}

.app-shell.hide-tutor .tutor-card,
.app-shell.hide-solver .solver-panel {
  display: none;
}

.tutor-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
}

.tutor-head h2 {
  margin: 0;
  font-size: 18px;
}

.tutor-head span {
  border: 1px solid #b9c9f2;
  border-radius: 999px;
  background: #fff;
  color: var(--accent-strong);
  padding: 4px 9px;
  font-size: 12px;
  font-weight: 700;
}

.tutor-head-actions {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

#tutorMessage {
  margin: 8px 0 6px;
  color: #23314a;
  font-weight: 700;
}

.action-coach {
  margin: 6px 0 8px;
  border-left: 4px solid var(--accent);
  background: #fff;
  color: #23314a;
  padding: 6px 9px;
  font-size: 13px;
}

.action-coach.good {
  border-left-color: var(--good);
}

.action-coach.bad {
  border-left-color: var(--bad);
}

.action-coach.warn {
  border-left-color: var(--warn);
}

.tutor-steps {
  margin: 0;
  padding-left: 22px;
  color: var(--muted);
}

.tutor-steps li {
  margin: 2px 0;
}

.tutor-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.tutor-actions button {
  min-height: 34px;
}

.tutor-card.is-floating,
.solver-panel.is-floating {
  position: fixed;
  z-index: 1000;
  width: clamp(320px, 34vw, 520px);
  max-height: calc(100vh - 24px);
  overflow: auto;
  margin: 0;
  padding: 10px;
  border: 1px solid #9fb6f1;
  border-radius: 8px;
  background: #f7faff;
  box-shadow: 0 18px 45px rgba(26, 43, 74, 0.24);
}

.tutor-card.is-floating .tutor-head,
.solver-panel.is-floating .solver-head {
  cursor: move;
  touch-action: none;
}

.solver-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  margin-bottom: 6px;
}

.solver-head h3 {
  margin: 0;
  font-size: 16px;
}

.solver-head span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.solver-head-actions {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

#tutorFloatBtn,
#solverFloatBtn {
  min-height: 28px;
  padding: 4px 9px;
  font-size: 12px;
  font-weight: 700;
}

.solver-steps {
  display: grid;
  gap: 5px;
  min-height: 28px;
  margin: 0;
  padding-left: 22px;
}

.solver-steps li {
  border-radius: 6px;
  color: var(--muted);
  padding: 3px 6px;
}

.solver-steps li.active {
  border: 1px solid #f59e0b;
  background: #fff7cc;
  box-shadow: inset 4px 0 0 #f59e0b;
  color: var(--ink);
  font-weight: 700;
}

.solver-steps li.done {
  color: var(--good);
}

.solver-actions {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 6px;
  margin-top: 8px;
}

.solver-actions button {
  min-height: 34px;
}

.work-area {
  display: contents;
}

.video-tutorial-section {
  grid-column: 1 / -1;
  grid-row: 3;
}

.tool-panel {
  grid-column: 1;
  grid-row: 2;
}

.model-board-wrap {
  grid-column: 2 / -1;
  grid-row: 2;
}

.math-panel {
  grid-column: 3;
  grid-row: 1;
}

.tool-panel,
.math-panel {
  padding: 10px;
}

.tool-panel h2,
.math-panel h2 {
  margin: 0 0 8px;
  font-size: 18px;
}

.tool-grid {
  display: grid;
  gap: 7px;
}

.tool-panel .button-row,
.math-panel .button-row {
  margin-top: 8px;
}

.tool-panel .button-row,
.math-panel .button-row {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.model-board-wrap {
  overflow: auto;
  padding: 10px;
}

.model-board {
  position: relative;
  min-height: 260px;
  min-width: min(620px, 100%);
  padding: 14px;
  border: 1px dashed #aab7ca;
  border-radius: 8px;
  background:
    linear-gradient(#e7eef9 1px, transparent 1px),
    linear-gradient(90deg, #e7eef9 1px, transparent 1px),
    #fbfdff;
  background-size: 24px 24px;
}

.model-board.solver-active {
  border-color: #6b8cff;
  background:
    linear-gradient(#dfe8fb 1px, transparent 1px),
    linear-gradient(90deg, #dfe8fb 1px, transparent 1px),
    #fbfdff;
  background-size: 24px 24px;
}

.solver-visual-note {
  display: grid;
  gap: 2px;
  margin-bottom: 12px;
  padding: 8px 10px;
  border: 1px solid #b9c9f2;
  border-left: 4px solid var(--accent);
  border-radius: 6px;
  background: #ffffff;
  color: #23314a;
  font-size: 13px;
}

.solver-visual-note strong {
  color: var(--accent-strong);
  font-size: 14px;
}

.empty-state {
  display: grid;
  place-items: center;
  height: 210px;
  color: var(--muted);
  text-align: center;
}

.model-row {
  display: grid;
  grid-template-columns: 108px minmax(0, 1fr) 30px 30px;
  gap: 8px;
  align-items: center;
  margin-bottom: 12px;
}

.model-board.solver-active .model-row {
  grid-template-columns: minmax(168px, 210px) minmax(0, 1fr) 30px 30px;
}

.model-row.solver-focus .model-label {
  border-color: var(--accent);
  background: #eff6ff;
  color: var(--accent-strong);
}

.model-label {
  width: 100%;
  min-width: 0;
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  padding: 6px 8px;
  text-align: center;
  font-weight: 700;
}

.bar {
  display: flex;
  min-height: 44px;
  border: 2px solid #23314a;
  border-radius: 5px;
  overflow: visible;
  background: #fff;
}

.segment {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 28px;
  border-right: 2px solid rgba(35, 49, 74, 0.72);
  color: #101828;
  font-weight: 700;
  text-align: center;
  cursor: pointer;
}

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

.segment.selected {
  outline: 3px solid #111827;
  outline-offset: -5px;
}

.segment.solver-total {
  border-color: #6b8cff;
  color: var(--accent-strong);
}

.segment.solver-base {
  color: #173b91;
}

.segment.solver-extra {
  border-color: #b45309;
  color: #7c2d12;
}

.segment.solver-remove {
  border-color: #b91c1c;
  color: #7f1d1d;
  text-decoration: line-through;
}

.segment.solver-spacer {
  visibility: hidden;
  pointer-events: none;
}

.segment.solver-answer {
  box-shadow: inset 0 0 0 3px #173b91;
  color: #0f2d73;
}

.segment span {
  box-sizing: border-box;
  width: 100%;
  max-width: 100%;
  overflow: hidden;
  overflow-wrap: normal;
  padding: 2px 4px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.segment.label-fitted span {
  text-overflow: clip;
}

.row-button {
  width: 30px;
  min-height: 30px;
  padding: 0;
  font-weight: 700;
}

#workingInput {
  min-height: 98px;
  font-family: Consolas, "Courier New", monospace;
}

.working-focus-line {
  margin-top: 8px;
  border: 1px solid #f59e0b;
  border-radius: 7px;
  background: #fff7cc;
  box-shadow: inset 4px 0 0 #f59e0b;
  color: #111827;
  font-family: Consolas, "Courier New", monospace;
  font-weight: 700;
  line-height: 1.35;
  padding: 8px 10px 8px 14px;
}

.working-feedback {
  min-height: 44px;
  margin: 8px 0;
  padding-left: 20px;
}

.working-feedback li {
  margin-bottom: 5px;
}

.working-feedback .good {
  color: var(--good);
}

.working-feedback .bad {
  color: var(--bad);
}

.calculator {
  display: grid;
  grid-template-columns: 1fr 44px;
  gap: 8px;
  align-items: center;
  margin-top: 8px;
}

.calculator output {
  grid-column: 1 / -1;
  min-height: 24px;
  color: var(--accent-strong);
  font-weight: 700;
}

@media (max-width: 1100px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .work-area {
    display: grid;
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .controls-panel,
  .question-panel,
  .tool-panel,
  .model-board-wrap,
  .math-panel,
  .video-tutorial-section {
    grid-column: auto;
    grid-row: auto;
  }

  .work-area {
    min-height: auto;
  }

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

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

@media (max-width: 700px) {
  .app-header {
    align-items: stretch;
    flex-direction: column;
    padding: 14px;
  }

  .score-card {
    text-align: left;
  }

  .app-shell {
    padding: 10px;
  }

  .question-panel h2 {
    font-size: 19px;
  }

  .model-board {
    min-width: 520px;
  }

  .solver-actions {
    grid-template-columns: 1fr;
  }

  .tutor-card.is-floating,
  .solver-panel.is-floating {
    width: calc(100vw - 20px);
  }
}

@media print {
  body {
    background: #fff;
  }

  .controls-panel,
  .tool-panel button,
  .math-panel button,
  .calculator {
    display: none;
  }

  .app-shell,
  .work-area {
    display: block;
  }

  .question-panel,
  .tool-panel,
  .math-panel,
  .model-board-wrap {
    box-shadow: none;
    margin-bottom: 12px;
  }
}
