:root {
  color-scheme: light;
  --bg: #eef3f8;
  --panel: #ffffff;
  --text: #162033;
  --muted: #64718a;
  --line: #dbe4f0;
  --blue: #2864f0;
  --blue-soft: #eaf1ff;
  --cell-head: #edf0f3;
  --cell-line: #d5dae2;
  --shadow: 0 12px 28px rgba(31, 45, 68, 0.12);
}

body.dark {
  color-scheme: dark;
  --bg: #101620;
  --panel: #172233;
  --text: #edf4ff;
  --muted: #a8b5c8;
  --line: #2b3a50;
  --blue: #77a5ff;
  --blue-soft: #20365f;
  --cell-head: #223047;
  --cell-line: #33435a;
  --shadow: 0 12px 28px rgba(0, 0, 0, 0.28);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: Arial, "Microsoft JhengHei", "PingFang TC", sans-serif;
}

.side-ad {
  position: fixed;
  top: 96px;
  z-index: 4;
  width: 160px;
  height: 600px;
}

.side-ad-left {
  left: max(12px, calc((100vw - 1880px) / 2));
}

.side-ad-right {
  right: max(12px, calc((100vw - 1880px) / 2));
}

button, textarea { font: inherit; }

svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px clamp(14px, 4vw, 34px);
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(12px);
}

.home-link, .top-actions, .source-actions, .section-head, .input-footer {
  display: flex;
  align-items: center;
}

.home-link {
  gap: 10px;
  color: var(--text);
  text-decoration: none;
  font-weight: 700;
}

.top-actions, .source-actions { gap: 10px; }

.icon-button {
  display: inline-grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border: 1px solid var(--line);
  border-radius: 50%;
  color: var(--blue);
  background: var(--panel);
  cursor: pointer;
  transition: transform 140ms ease, box-shadow 140ms ease, background-color 140ms ease, border-color 140ms ease, color 140ms ease;
}

.lang-mark {
  font-weight: 800;
  font-size: 15px;
}

body.dark .moon-icon, body:not(.dark) .sun-icon { display: none; }

.shell {
  width: min(1520px, 100%);
  margin: 0 auto;
  padding: clamp(16px, 3vw, 32px);
}

.mode-tabs {
  display: inline-flex;
  gap: 8px;
  padding: 6px;
  margin-bottom: 18px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.mode-tab {
  min-height: 42px;
  padding: 0 18px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-weight: 700;
  transition: transform 140ms ease, box-shadow 140ms ease, background-color 140ms ease, color 140ms ease;
}

.mode-tab.active {
  background: var(--blue);
  color: #fff;
}

.format-tabs {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 12px;
  margin-bottom: 18px;
}

.format-tab {
  min-height: 42px;
  padding: 0 20px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--panel);
  color: var(--muted);
  cursor: pointer;
  font-weight: 700;
  white-space: nowrap;
  transition: transform 140ms ease, box-shadow 140ms ease, background-color 140ms ease, border-color 140ms ease, color 140ms ease;
}

.format-tab.active {
  border-color: var(--blue);
  background: var(--blue);
  color: #fff;
}

.mode-panel {
  display: none;
}

.mode-panel.active {
  display: block;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: clamp(18px, 3vw, 28px);
}

.panel + .panel { margin-top: 28px; }

.section-head {
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 18px;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
  text-transform: uppercase;
}

h1, h2 {
  margin: 0;
  line-height: 1.15;
}

h1 { font-size: clamp(26px, 4vw, 42px); }
h2 { font-size: clamp(22px, 3vw, 30px); }

#sizeBadge {
  margin-left: 10px;
  color: var(--blue);
  font-size: 18px;
  font-weight: 600;
}

.primary-btn, .outline-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  min-height: 46px;
  padding: 0 20px;
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
  transition: transform 140ms ease, box-shadow 140ms ease, background-color 140ms ease, border-color 140ms ease, color 140ms ease;
}

.primary-btn {
  border: 1px solid var(--blue);
  background: var(--blue);
  color: #fff;
  box-shadow: 0 10px 20px rgba(40, 100, 240, 0.2);
}

.outline-btn {
  border: 1px solid var(--blue);
  background: transparent;
  color: var(--blue);
}

.primary-btn:hover, .mode-tab.active:hover, .format-tab.active:hover {
  filter: brightness(1.05);
  transform: translateY(-1px);
  box-shadow: 0 14px 26px rgba(40, 100, 240, 0.28);
}

.outline-btn:hover, .icon-button:hover, .mode-tab:not(.active):hover, .format-tab:not(.active):hover {
  background: var(--blue-soft);
  border-color: var(--blue);
  color: var(--blue);
  transform: translateY(-1px);
  box-shadow: 0 10px 20px rgba(40, 100, 240, 0.12);
}

.primary-btn:active, .outline-btn:active, .icon-button:active, .mode-tab:active, .format-tab:active, .tool-btn:active {
  transform: translateY(1px) scale(0.98);
  box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.18);
}

.primary-btn:focus-visible, .outline-btn:focus-visible, .icon-button:focus-visible, .mode-tab:focus-visible, .format-tab:focus-visible, .tool-btn:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--blue) 45%, transparent);
  outline-offset: 3px;
}

.is-clicked {
  animation: clickPulse 320ms ease;
}

.is-success {
  border-color: #21a67a !important;
  background: #21a67a !important;
  color: #fff !important;
}

.compact { min-height: 40px; }

input[type="file"] { display: none; }

.grid-actions {
  flex-wrap: wrap;
  justify-content: flex-end;
}

.excel-methods {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr);
  gap: 18px;
  align-items: stretch;
}

.method-block {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-width: 0;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: color-mix(in srgb, var(--panel) 94%, var(--blue-soft));
}

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

.method-head h2 {
  font-size: 20px;
  line-height: 1.25;
}

.method-actions {
  display: flex;
  justify-content: flex-end;
}

.blank-grid-maker {
  display: grid;
  gap: 14px;
  align-content: start;
}

.output-workspace {
  display: grid;
  grid-template-columns: minmax(280px, 360px) minmax(0, 1fr);
  gap: 24px;
  align-items: stretch;
}

.format-options {
  padding-right: 22px;
  border-right: 1px solid var(--line);
}

.option-group {
  display: none;
}

.option-group.active {
  display: grid;
  gap: 14px;
}

.check-field {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  font-weight: 600;
}

.check-field input {
  width: 22px;
  height: 22px;
  accent-color: var(--blue);
}

.stack-field {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-weight: 700;
}

.stack-field input, .stack-field select {
  min-height: 42px;
  width: 100%;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  font: inherit;
}

.two-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.number-field {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  background: var(--panel);
  font-weight: 700;
}

.number-field input {
  width: 74px;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-weight: 700;
}

#jsonInput, #excelInput, .code-output {
  width: 100%;
  min-height: 250px;
  resize: vertical;
  padding: 22px;
  border: 0;
  border-radius: 8px;
  outline: 2px solid transparent;
  background: #35445a;
  color: #f8fbff;
  font-family: Consolas, "Courier New", monospace;
  font-size: 16px;
  line-height: 1.55;
}

#excelInput {
  min-height: 170px;
  background: #263446;
}

.code-output {
  min-height: 330px;
  background: #182232;
}

#jsonInput:focus, #excelInput:focus, .code-output:focus { outline-color: var(--blue); }

.input-footer {
  justify-content: space-between;
  gap: 14px;
  margin-top: 14px;
  color: var(--muted);
}

.input-footer p { margin: 0; }

.editor-grid {
  display: grid;
  grid-template-columns: 390px minmax(0, 1fr);
  gap: 28px;
  align-items: start;
}

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

.tool-btn {
  display: grid;
  place-items: center;
  gap: 8px;
  min-height: 94px;
  padding: 12px 8px;
  border: 1px solid #cfe0ff;
  border-radius: 8px;
  background: var(--panel);
  color: var(--blue);
  cursor: pointer;
  transition: transform 140ms ease, box-shadow 140ms ease, background-color 140ms ease, border-color 140ms ease, color 140ms ease;
}

.tool-btn strong {
  font-size: 24px;
  line-height: 1;
}

.tool-btn:hover {
  background: var(--blue-soft);
  border-color: var(--blue);
  transform: translateY(-2px);
  box-shadow: 0 10px 20px rgba(40, 100, 240, 0.12);
}

@keyframes clickPulse {
  0% { transform: scale(1); }
  45% { transform: scale(0.96); }
  100% { transform: scale(1); }
}

.tool-btn.danger { color: #ff3d47; }
.tool-btn.warn { color: #f36d21; }
.tool-btn.purple { color: #8457ff; }

.table-wrap {
  overflow: auto;
  max-height: 620px;
  min-height: 360px;
  border: 1px solid var(--cell-line);
  background: var(--panel);
}

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

th, td {
  min-width: 150px;
  max-width: 320px;
  height: 42px;
  padding: 8px 10px;
  border: 1px solid var(--cell-line);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--cell-head);
  text-align: left;
  font-weight: 700;
}

.row-head {
  position: sticky;
  left: 0;
  z-index: 2;
  min-width: 54px;
  width: 54px;
  max-width: 54px;
  text-align: center;
  background: var(--cell-head);
}

td:focus {
  outline: 2px solid var(--blue);
  outline-offset: -2px;
}

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

@media (max-width: 1880px) {
  .shell {
    width: min(1320px, calc(100% - 380px));
  }
}

@media (max-width: 1280px) {
  .side-ad {
    display: none;
  }

  .shell {
    width: min(1520px, 100%);
  }
}

@media (max-width: 980px) {
  .editor-grid { grid-template-columns: 1fr; }
  .tools { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .excel-methods { grid-template-columns: 1fr; }
  .output-workspace { grid-template-columns: 1fr; }
  .format-options {
    padding-right: 0;
    padding-bottom: 18px;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
}

@media (max-width: 640px) {
  .topbar, .section-head, .input-footer {
    align-items: stretch;
    flex-direction: column;
  }

  .topbar {
    position: static;
  }

  .top-actions, .source-actions {
    justify-content: space-between;
    flex-wrap: wrap;
  }

  .mode-tabs {
    display: flex;
    width: 100%;
  }

  .mode-tab {
    flex: 1;
    padding: 0 10px;
  }

  .number-field {
    flex: 1;
    justify-content: space-between;
  }

  .method-head, .method-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .blank-grid-maker .primary-btn {
    width: 100%;
  }

  .two-fields {
    grid-template-columns: 1fr;
  }

  .home-link { justify-content: center; }
  .editor-head .primary-btn { width: 100%; }
  .tools { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .tool-btn { min-height: 84px; }
  th, td { min-width: 128px; }
}
