@font-face {
  font-family: "Roboto";
  src: url("../fonts/Roboto-Variable.ttf") format("truetype");
  font-weight: 100 900;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "Tinos";
  src: url("../fonts/Tinos-BoldItalic.ttf") format("truetype");
  font-weight: 700;
  font-style: italic;
  font-display: swap;
}

:root {
  --bg: #080a0d;
  --surface: #101318;
  --surface-raised: #171b21;
  --surface-hover: #1d222a;
  --line: #292f38;
  --line-strong: #3a424e;
  --text: #f4f5f6;
  --muted: #9299a3;
  --subtle: #69717c;
  --gold: #8471e8;
  --gold-strong: #9fb8ff;
  --gold-soft: rgba(132, 113, 232, 0.14);
  --blue: #4b8cff;
  --danger: #e57373;
  --radius: 8px;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  min-width: 320px;
  min-height: 100%;
  background: var(--bg);
}

body {
  min-height: 100vh;
  color: var(--text);
  background: var(--bg);
  font-family: "Roboto", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 14px;
  letter-spacing: 0;
}

button,
input {
  font: inherit;
  letter-spacing: 0;
}

button {
  -webkit-tap-highlight-color: transparent;
}

.app-shell {
  width: min(1600px, calc(100% - 24px));
  min-height: calc(100vh - 24px);
  margin: 12px auto;
  display: grid;
  grid-template-columns: 280px minmax(460px, 1fr) 300px;
  gap: 12px;
}

.panel {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.control-panel,
.editor-panel {
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.brand-lockup {
  min-height: 54px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--line);
}

.brand-mark {
  width: 40px;
  height: 40px;
  flex: 0 0 40px;
  display: grid;
  place-items: center;
  border: 1px solid #4d69ee;
  border-radius: 6px;
  background: #10142a;
  color: var(--gold-strong);
  font-size: 20px;
  font-weight: 900;
}

.brand-lockup div,
.editor-header,
.preview-title,
.cache-strip > div:first-child {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.brand-lockup strong {
  color: var(--gold-strong);
  font-size: 17px;
  font-weight: 800;
}

.brand-lockup span:last-child,
.editor-header span,
.preview-title span,
.cache-strip span,
.section-heading span,
.template-summary span,
.status-bar span:last-child,
.field-label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
}

.tool-section {
  display: flex;
  flex-direction: column;
  gap: 11px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #12161b;
}

.section-heading,
.preview-header,
.status-bar,
.adjust-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.section-heading strong,
.editor-header strong,
.preview-title strong {
  color: var(--text);
  font-size: 14px;
  font-weight: 700;
}

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

.language-button,
.level-button,
.button,
.icon-button,
.file-picker {
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  background: var(--surface-raised);
  transition: border-color 160ms ease, background 160ms ease, color 160ms ease, transform 160ms ease;
}

.language-button,
.level-button,
.button,
.icon-button {
  cursor: pointer;
}

.language-button {
  min-width: 0;
  min-height: 62px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 3px;
  text-align: left;
}

.language-button strong {
  overflow: hidden;
  color: var(--text);
  font-size: 13px;
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.language-button span {
  color: var(--muted);
  font-size: 11px;
}

.language-button:hover,
.level-button:hover,
.button:hover,
.icon-button:hover,
.file-picker:hover {
  border-color: var(--line-strong);
  background: var(--surface-hover);
}

.language-button.active,
.level-button.active {
  border-color: #4d69ee;
  background: var(--gold-soft);
  color: var(--gold-strong);
}

.language-button.active strong,
.language-button.active span {
  color: var(--gold-strong);
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 6px;
}

.level-button {
  min-width: 0;
  height: 36px;
  padding: 0 4px;
  font-size: 11px;
  font-weight: 700;
}

.template-summary {
  margin-top: auto;
  gap: 5px;
}

.template-summary strong {
  color: var(--gold-strong);
  font-size: 14px;
}

.preview-panel {
  min-height: 0;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow: hidden;
}

.preview-header {
  min-height: 44px;
}

.action-row {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 7px;
}

.button {
  min-height: 38px;
  padding: 8px 13px;
  font-size: 12px;
  font-weight: 700;
}

.button-primary {
  border-color: #4d69ee;
  background: var(--gold);
  color: #080a0d;
}

.button-primary:hover {
  border-color: var(--gold-strong);
  background: var(--gold-strong);
}

.button:active,
.icon-button:active,
.language-button:active,
.level-button:active {
  transform: translateY(1px);
}

.cache-strip {
  min-height: 50px;
  display: grid;
  grid-template-columns: minmax(150px, 1fr) minmax(100px, 220px) 46px;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #0d1014;
}

.cache-strip strong,
.cache-strip b {
  color: var(--text);
  font-size: 12px;
  font-weight: 700;
}

.cache-strip b {
  text-align: right;
}

.cache-meter {
  height: 5px;
  overflow: hidden;
  border-radius: 3px;
  background: #252a31;
}

.cache-meter span {
  display: block;
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: var(--gold);
  transition: width 180ms ease;
}

.canvas-stage {
  min-height: 0;
  flex: 1 1 auto;
  position: relative;
  display: grid;
  place-items: start center;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 6px;
  background:
    linear-gradient(45deg, #0c0f13 25%, transparent 25%),
    linear-gradient(-45deg, #0c0f13 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #0c0f13 75%),
    linear-gradient(-45deg, transparent 75%, #0c0f13 75%),
    #0f1216;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0;
  background-size: 16px 16px;
}

#certificateCanvas {
  display: block;
  width: auto;
  height: min(100%, 78vh);
  max-height: 100%;
  max-width: 100%;
  aspect-ratio: 941 / 1672;
  object-fit: contain;
  cursor: grab;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.42);
}

#certificateCanvas:active {
  cursor: grabbing;
}

.state-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 12px;
  color: var(--text);
  background: rgba(8, 10, 13, 0.82);
  backdrop-filter: blur(4px);
}

.state-overlay.hidden,
.hidden {
  display: none !important;
}

.spinner {
  width: 26px;
  height: 26px;
  border: 2px solid #373d46;
  border-top-color: var(--gold);
  border-radius: 50%;
  animation: spin 760ms linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.status-bar {
  min-height: 22px;
  color: var(--text);
  font-size: 12px;
}

.editor-header {
  min-height: 54px;
  justify-content: center;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--line);
}

.file-picker {
  min-height: 42px;
  padding: 10px 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  cursor: pointer;
  text-align: center;
  text-wrap: pretty;
}

.file-picker input {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  opacity: 0;
}

.field-label {
  display: block;
}

.text-input {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px 11px;
  outline: none;
  background: #0c0f13;
  color: var(--text);
  font-weight: 600;
}

.text-input::placeholder {
  color: #5f6670;
}

.text-input:focus {
  border-color: #4d69ee;
  box-shadow: 0 0 0 3px rgba(132, 113, 232, 0.14);
}

.adjust-row {
  justify-content: center;
}

.icon-button {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  flex: 0 0 36px;
  padding: 0;
  font-size: 17px;
  font-weight: 700;
}

.nudge-grid {
  width: 102px;
  height: 74px;
  display: grid;
  grid-template-columns: repeat(3, 30px);
  grid-template-rows: repeat(2, 34px);
  gap: 4px 6px;
  justify-content: center;
}

.nudge-grid .icon-button {
  width: 30px;
  height: 34px;
  min-height: 34px;
  font-size: 14px;
}

.nudge-up { grid-column: 2; grid-row: 1; }
.nudge-left { grid-column: 1; grid-row: 2; }
.nudge-down { grid-column: 2; grid-row: 2; }
.nudge-right { grid-column: 3; grid-row: 2; }

.coordinate-readout {
  margin-top: auto;
  display: grid;
  gap: 5px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #0c0f13;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 10px;
  line-height: 1.5;
}

.coordinate-readout strong {
  color: #c5c9cf;
  font-weight: 600;
}

:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}

@media (max-width: 1180px) {
  .app-shell {
    grid-template-columns: 250px minmax(430px, 1fr);
  }

  .editor-panel {
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    align-items: start;
  }

  .editor-header,
  .coordinate-readout {
    grid-column: 1 / -1;
  }
}

@media (max-width: 760px) {
  .app-shell {
    width: min(100% - 16px, 620px);
    margin: 8px auto;
    display: flex;
    flex-direction: column;
  }

  .control-panel,
  .editor-panel {
    display: flex;
  }

  .template-summary {
    margin-top: 0;
  }

  .preview-panel {
    min-height: auto;
  }

  .preview-header {
    align-items: flex-start;
    flex-direction: column;
  }

  .action-row {
    width: 100%;
  }

  .action-row .button {
    flex: 1 1 0;
  }

  .cache-strip {
    grid-template-columns: 1fr auto;
  }

  .cache-meter {
    grid-column: 1 / -1;
    grid-row: 2;
  }

  .canvas-stage {
    width: 100%;
    min-height: 0;
    aspect-ratio: 941 / 1672;
  }

  #certificateCanvas {
    width: 100%;
    height: auto;
    max-height: none;
  }
}

@media (max-width: 420px) {
  .control-panel,
  .editor-panel,
  .preview-panel {
    padding: 12px;
  }

  .action-row {
    flex-wrap: wrap;
  }

  .action-row .button:first-child {
    flex-basis: 100%;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}
