:root {
  color-scheme: light;
  --ink: #20242a;
  --muted: #667085;
  --line: #d9e0e7;
  --paper: #fbfcfb;
  --panel: #ffffff;
  --cell: #f4f7f6;
  --mint: #2f9b8f;
  --mint-dark: #197367;
  --coral: #ec6a5c;
  --gold: #f2b84b;
  --violet: #6750a4;
  --blue: #2f6fb3;
}

body.dark {
  color-scheme: dark;
  --ink: #f3f7f6;
  --muted: #b7c1c9;
  --line: #35414a;
  --paper: #12171a;
  --panel: #1b2227;
  --cell: #232d33;
  --mint: #54c7b8;
  --mint-dark: #79ddcf;
  --coral: #ff8a7d;
  --gold: #f4c86b;
  --violet: #b8a6ff;
  --blue: #83baf4;
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  font-family: "Microsoft JhengHei", "Noto Sans TC", "PingFang TC", Arial, sans-serif;
  background: var(--paper);
  color: var(--ink);
}

a {
  color: inherit;
  text-decoration: none;
}

button,
input,
select {
  font: inherit;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: 48px minmax(0, 1fr) 48px;
  align-items: center;
  gap: 18px;
  min-height: 68px;
  padding: 12px clamp(16px, 4vw, 54px);
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  backdrop-filter: blur(16px);
}

.brand {
  display: flex;
  justify-self: center;
  align-items: center;
  gap: 10px;
  font-size: 20px;
  font-weight: 900;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 8px;
  background: var(--mint);
  color: #fff;
  font-weight: 900;
}

.header-tools {
  display: flex;
  justify-self: end;
  align-items: center;
  gap: clamp(10px, 2vw, 24px);
  color: var(--muted);
  font-size: 15px;
}

.side-nav a:hover {
  color: var(--coral);
}

.menu-button,
.icon-button {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--panel);
  color: var(--ink);
  cursor: pointer;
  font-size: 21px;
  line-height: 1;
}

.menu-button {
  visibility: hidden;
  border-radius: 8px;
}

.sidebar-overlay {
  display: none;
}

.workspace {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
  gap: 20px;
  padding: clamp(20px, 4vw, 42px) clamp(16px, 4vw, 54px) clamp(42px, 7vw, 92px);
}

.sidebar,
.function-panel,
.lesson-grid article,
.error-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.sidebar {
  position: sticky;
  top: 86px;
  align-self: start;
  padding: 18px;
}

.sidebar-head {
  display: none;
}

.side-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 18px;
  padding-bottom: 18px;
  border-bottom: 1px solid var(--line);
}

.side-nav-row {
  display: contents;
}

.side-nav a {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  min-height: 36px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--cell);
  color: var(--mint-dark);
  font-weight: 800;
  white-space: nowrap;
}

.search-label {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
}

.select-label {
  margin-top: 16px;
}

input,
select {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--ink);
}

input {
  padding: 0 14px;
}

select {
  padding: 0 12px;
}

.function-buttons {
  display: grid;
  gap: 8px;
  margin-top: 16px;
  max-height: calc(100vh - 260px);
  overflow: auto;
}

.function-button {
  width: 100%;
  min-height: 42px;
  padding: 0 12px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: var(--cell);
  color: var(--ink);
  text-align: left;
  cursor: pointer;
}

.function-button.active {
  border-color: rgba(47, 155, 143, 0.65);
  background: color-mix(in srgb, var(--mint) 18%, var(--panel));
  color: var(--mint-dark);
  font-weight: 800;
}

.function-panel {
  min-width: 0;
  min-height: 640px;
  padding: clamp(18px, 4vw, 36px);
}

.adsense-wrap {
  margin-top: 24px;
  min-height: 90px;
}

.eyebrow {
  margin: 0 0 10px;
  color: var(--coral);
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 10px;
  color: var(--mint-dark);
  font-size: clamp(34px, 5vw, 56px);
  line-height: 1.05;
  letter-spacing: 0;
}

h2 {
  margin-bottom: 8px;
  font-size: 18px;
  line-height: 1.25;
  letter-spacing: 0;
}

#functionTagline {
  max-width: 780px;
  color: var(--muted);
  font-size: 18px;
  line-height: 1.7;
}

.formula-box {
  display: grid;
  gap: 8px;
  margin: 22px 0;
  padding: 18px;
  border-left: 5px solid var(--gold);
  border-radius: 8px;
  background: color-mix(in srgb, var(--gold) 17%, var(--panel));
}

.formula-box span {
  color: color-mix(in srgb, var(--gold) 55%, var(--ink));
  font-size: 13px;
  font-weight: 800;
}

code {
  font-family: Consolas, "Courier New", monospace;
  white-space: normal;
  word-break: break-word;
}

.lesson-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
}

.lesson-grid article {
  min-height: 148px;
  padding: 18px;
  background: var(--cell);
}

.lesson-grid p,
.example-runner p,
.copy-status,
.error-card p,
footer p {
  color: var(--muted);
  line-height: 1.7;
}

.example-runner {
  display: grid;
  grid-template-columns: minmax(360px, 1fr) minmax(260px, 0.72fr);
  gap: 18px;
  align-items: start;
  min-width: 0;
  margin-top: 16px;
  padding: 18px;
  border-radius: 8px;
  background: color-mix(in srgb, var(--blue) 10%, var(--panel));
}

.example-copy-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.copy-button {
  width: 100%;
  min-height: 38px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--ink);
  cursor: pointer;
  margin-top: 12px;
}

.result-box {
  display: grid;
  gap: 12px;
  width: 100%;
  margin-top: 16px;
  padding: 18px;
  border-radius: 8px;
  background: var(--panel);
}

.result-box output {
  color: var(--blue);
  font-size: 28px;
  font-weight: 900;
}

.example-sheet-wrap {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.example-sheet {
  display: grid;
  min-width: 420px;
  width: max-content;
  grid-template-columns: 54px repeat(var(--example-cols), minmax(160px, max-content));
  border-top: 1px solid var(--line);
  border-left: 1px solid var(--line);
  overflow: hidden;
  border-radius: 8px;
}

.example-cell {
  display: grid;
  align-items: center;
  min-height: 42px;
  padding: 6px 10px;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  font-size: 14px;
  white-space: nowrap;
}

.example-cell.header,
.example-cell.row-head {
  background: var(--cell);
  color: var(--muted);
  font-weight: 800;
}

.example-cell.formula {
  background: color-mix(in srgb, var(--gold) 17%, var(--panel));
  color: color-mix(in srgb, var(--gold) 55%, var(--ink));
  font-family: Consolas, "Courier New", monospace;
}

.example-cell.answer {
  background: color-mix(in srgb, var(--mint) 18%, var(--panel));
  color: var(--mint-dark);
  font-weight: 900;
}

.error-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
  margin-top: 22px;
}

.error-card {
  min-height: 180px;
  padding: 18px;
}

.error-card strong {
  display: inline-flex;
  margin-bottom: 12px;
  padding: 6px 10px;
  border-radius: 8px;
  background: color-mix(in srgb, var(--blue) 13%, var(--panel));
  color: var(--violet);
  font-family: Consolas, "Courier New", monospace;
}

footer {
  padding: 26px clamp(16px, 4vw, 54px);
  border-top: 1px solid var(--line);
  background: var(--panel);
}

footer p {
  margin: 0;
}

footer a {
  color: var(--mint-dark);
  font-weight: 800;
}

footer a:hover {
  color: var(--coral);
}

@media (max-width: 1040px) {
  .topbar,
  .example-runner {
    grid-template-columns: 1fr;
  }

  .brand,
  .header-tools {
    justify-self: start;
  }

  .topbar {
    grid-template-columns: 48px minmax(0, 1fr) 48px;
  }

  .brand {
    justify-self: center;
  }

  .header-tools {
    justify-self: end;
  }

  .sidebar {
    position: sticky;
  }

  .function-buttons {
    max-height: 260px;
  }

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

@media (max-width: 680px) {
  body.menu-open {
    overflow: hidden;
  }

  .topbar {
    gap: 10px;
    min-height: 62px;
    padding: 10px 14px;
  }

  .menu-button {
    visibility: visible;
  }

  .brand {
    font-size: 17px;
    gap: 8px;
    min-width: 0;
  }

  .brand-mark {
    width: 34px;
    height: 34px;
  }

  .brand span:last-child {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .header-tools {
    gap: 0;
  }

  .workspace {
    display: block;
    padding: 14px;
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 40;
    width: min(86vw, 340px);
    padding: 16px;
    overflow-y: auto;
    border-radius: 0;
    transform: translateX(-104%);
    transition: transform 180ms ease;
  }

  body.menu-open .sidebar {
    transform: translateX(0);
    box-shadow: 18px 0 40px rgba(0, 0, 0, 0.18);
  }

  .sidebar-overlay {
    position: fixed;
    inset: 0;
    z-index: 35;
    display: block;
    border: 0;
    background: rgba(20, 28, 32, 0.38);
    opacity: 0;
    pointer-events: none;
    transition: opacity 180ms ease;
  }

  body.menu-open .sidebar-overlay {
    opacity: 1;
    pointer-events: auto;
  }

  .sidebar-head {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    min-height: 38px;
    margin-bottom: 4px;
    color: var(--muted);
    font-size: 14px;
    font-weight: 800;
  }

  .sidebar-close {
    display: grid;
    place-items: center;
    width: 38px;
    height: 38px;
    border: 1px solid var(--line);
    border-radius: 50%;
    background: var(--panel);
    color: var(--ink);
    cursor: pointer;
    font-size: 24px;
    line-height: 1;
  }

  .function-buttons {
    max-height: min(46vh, 380px);
    overflow-y: auto;
    padding-right: 2px;
    -webkit-overflow-scrolling: touch;
  }

  .function-panel {
    padding: 20px 14px;
  }

  .lesson-grid,
  .error-grid {
    grid-template-columns: 1fr;
  }

  .example-runner {
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: 100%;
    overflow: hidden;
    padding: 12px;
  }

  .example-copy {
    display: contents;
  }

  .example-copy-heading {
    order: 1;
  }

  #exampleText {
    order: 2;
  }

  .example-sheet-wrap {
    order: 3;
    align-self: stretch;
  }

  .result-box {
    order: 4;
  }

  .copy-button {
    order: 5;
  }

  .copy-status {
    order: 6;
  }

  .example-sheet {
    min-width: 520px;
  }
}
