@import url(
  "https://fonts.googleapis.com/css2?family=DM+Mono:ital@0;1&family=DM+Sans:ital,opsz,wght@0,9..40,100..1000;1,9..40,100..1000&family=Space+Grotesk:wght@400;700&display=swap"
);

:root {
  /* Colors */
  --color-midnight-blue: #131c2d;
  --color-butter-yellow: #ffeda8;
  --color-olive-green: #576238;
  --color-bright-yellow: #ffef5f;
  --color-white: #ffffff;

  /* Typography */
  --font-display: "Space Grotesk", sans-serif;
  --font-body: "DM Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: "DM Mono", ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;

  /* UI Elements (Derived from rules) */
  --bg-main: var(--color-midnight-blue);
  --text-main: var(--color-butter-yellow);
  --bg-card: var(--color-midnight-blue);
  --text-card: var(--color-butter-yellow);
  --btn-primary-bg: var(--color-bright-yellow);
  --btn-primary-text: var(--color-midnight-blue);

  --text-fine: var(--color-white);
  --text-accent-olive: var(--color-olive-green);
  --radius-sm: 0;
  --radius-md: 0;
  --radius-lg: 0;
  --border-soft-light: rgba(255, 237, 168, 0.35);
  --border-soft-dark: rgba(19, 28, 45, 0.25);

  /* Spacing scale (multiples of 4px) */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;

  /* Buttons: one type ramp for all variants */
  --btn-font-size: 14px;
  --btn-line-height: 1.25;
  --btn-font-weight: 700;
  --btn-padding-y: 12px;
  --btn-padding-x: 16px;
  --btn-gap: var(--space-2);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: var(--font-body);
  background: var(--bg-main);
  color: var(--text-main);
  -webkit-text-size-adjust: 100%;
}

h1,
h2,
h3 {
  font-family: var(--font-display);
  line-height: 1.2;
  margin: 0;
}

h1 {
  font-size: clamp(1.25rem, 2.2vw, 1.75rem);
  color: var(--text-main);
}

h2,
h3 {
  color: var(--color-midnight-blue);
}

code {
  font-family: var(--font-mono);
}

.theme-dark {
  background: var(--color-midnight-blue);
  color: var(--color-butter-yellow);
}

.theme-dark .text-primary {
  color: var(--color-butter-yellow);
}

.theme-dark .text-secondary {
  color: var(--color-white);
}

.theme-dark .text-accent {
  color: var(--color-olive-green);
}

.theme-light {
  background: var(--color-butter-yellow);
  color: var(--color-olive-green);
}

.theme-light .text-primary {
  color: var(--color-olive-green);
}

.theme-light .text-secondary {
  color: var(--color-midnight-blue);
}

.theme-light .text-accent {
  color: var(--color-olive-green);
}

.wrap {
  max-width: 1100px;
  padding: var(--space-6);
  padding-left: max(var(--space-6), env(safe-area-inset-left));
  padding-right: max(var(--space-6), env(safe-area-inset-right));
  padding-bottom: max(var(--space-6), env(safe-area-inset-bottom));
  margin: 0 auto;
  display: grid;
  gap: var(--space-4);
}

@media (max-width: 599px) {
  .wrap {
    padding: var(--space-4);
    padding-left: max(var(--space-4), env(safe-area-inset-left));
    padding-right: max(var(--space-4), env(safe-area-inset-right));
    padding-bottom: max(var(--space-4), env(safe-area-inset-bottom));
    gap: var(--space-4);
  }
}

header {
  display: flex;
  gap: var(--space-2);
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
}

header .header-actions,
header .actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-wrap: wrap;
}

/* Creator: log out first on narrow screens */
.creator-header {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: var(--space-3);
}

.creator-header-text {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  min-width: 0;
}

.creator-header-actions {
  order: -1;
  align-self: flex-end;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-2);
}

@media (min-width: 900px) {
  .creator-header {
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
  }

  .creator-header-actions {
    order: 0;
    align-self: center;
    flex: 0 0 auto;
  }

  .creator-header-text {
    flex: 1;
    min-width: 0;
  }
}

/* Dashboard: action row first on narrow screens */
.dashboard-header {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: var(--space-3);
}

.dashboard-header-text {
  min-width: 0;
}

.dashboard-header .actions {
  order: -1;
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  justify-content: flex-end;
}

@media (max-width: 599px) {
  .dashboard-header .actions {
    flex-direction: column;
    align-items: stretch;
  }

  .dashboard-header .actions > button,
  .dashboard-header .actions > a.btn {
    width: 100%;
    justify-content: center;
  }
}

@media (min-width: 900px) {
  .dashboard-header {
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
  }

  .dashboard-header .actions {
    order: 0;
    width: auto;
    justify-content: flex-end;
  }

  .dashboard-header .actions > button,
  .dashboard-header .actions > a.btn {
    width: auto;
  }
}

.note,
.hint {
  color: var(--text-fine);
  font-size: 12px;
  line-height: 1.4;
}

.grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--space-4);
}

@media (max-width: 899px) {
  .grid.creator-grid {
    gap: var(--space-3);
  }
}

@media (min-width: 900px) {
  .grid.creator-grid {
    grid-template-columns: 420px 1fr;
    align-items: start;
  }
}

.card {
  background: var(--bg-card);
  color: var(--text-card);
  border: 1px solid var(--border-soft-light);
  border-radius: var(--radius-lg);
  padding: var(--space-4);
}

@media (max-width: 599px) {
  .card {
    padding: var(--space-3);
  }
}

.creator-panel {
  background: var(--color-midnight-blue);
  color: var(--color-butter-yellow);
  border: 1px solid var(--border-soft-light);
}

label {
  display: block;
  font-size: 12px;
  color: var(--color-butter-yellow);
  margin-bottom: var(--space-2);
}

.theme-light label {
  color: var(--color-olive-green);
}

input[type="file"],
input[type="email"],
input[type="password"] {
  width: 100%;
  padding: var(--space-3);
  border-radius: var(--radius-sm);
  border: 1px solid rgba(87, 98, 56, 0.35);
  background: var(--color-white);
  color: var(--color-midnight-blue);
  font-size: var(--btn-font-size);
  line-height: var(--btn-line-height);
  font-family: var(--font-body);
}

button,
a.btn {
  appearance: none;
  border: 1px solid var(--border-soft-dark);
  background: var(--btn-primary-bg);
  color: var(--btn-primary-text);
  padding: var(--btn-padding-y) var(--btn-padding-x);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: var(--btn-font-size);
  line-height: var(--btn-line-height);
  font-weight: var(--btn-font-weight);
  font-family: var(--font-body);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--btn-gap);
}

button.btn-block,
a.btn.btn-block {
  width: 100%;
  justify-content: center;
}

@media (hover: none) and (pointer: coarse) {
  button,
  a.btn {
    min-height: 44px;
  }
}

button:hover,
a.btn:hover {
  filter: brightness(0.96);
}

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

button.secondary,
a.btn.secondary,
button.linkish {
  background: var(--color-midnight-blue);
  color: var(--color-butter-yellow);
  border-color: var(--border-soft-light);
}

button.danger {
  background: var(--color-white);
  color: var(--color-midnight-blue);
}

.btnrow {
  display: flex;
  gap: var(--space-2);
  flex-wrap: wrap;
}

@media (max-width: 599px) {
  .creator-panel .btnrow {
    flex-direction: column;
    align-items: stretch;
  }

  .creator-panel .btnrow > button {
    width: 100%;
    justify-content: center;
  }
}

.controls {
  display: grid;
  gap: var(--space-3);
}

.error,
.status,
.msg {
  font-size: 12px;
  min-height: var(--space-4);
  color: var(--color-midnight-blue);
}

.error.success,
.status.ok,
.msg.ok {
  color: var(--color-olive-green);
}

.status.err,
.msg.err,
.error.err {
  color: #9f1d1d;
}

.theme-dark .error {
  color: rgba(255, 210, 210, 0.95);
}

.theme-dark .error.success {
  color: var(--color-bright-yellow);
}

.theme-dark .status.ok {
  color: var(--color-bright-yellow);
}

.theme-dark .status.err {
  color: #ff9a9a;
}

.preview {
  display: grid;
  gap: var(--space-2);
  justify-items: stretch;
}

.preview-row {
  display: flex;
  flex-direction: row;
  gap: var(--space-3);
  align-items: flex-start;
  width: 100%;
}

@media (max-width: 899px) {
  .preview-row {
    flex-direction: row;
    align-items: flex-start;
    gap: var(--space-2);
  }
}

.templates-rail {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  gap: var(--space-2);
  overflow-x: auto;
  overflow-y: hidden;
  flex: 0 0 auto;
  max-width: min(200px, 32vw);
  padding: var(--space-1) var(--space-1) var(--space-2);
  scrollbar-width: thin;
}

@media (max-width: 899px) {
  .templates-rail {
    flex-direction: column;
    flex-wrap: nowrap;
    align-items: center;
    max-width: none;
    width: auto;
    overflow-x: visible;
    overflow-y: visible;
    padding: var(--space-1) 0 var(--space-2);
    gap: var(--space-2);
  }
}

.thumb-btn {
  flex: 0 0 auto;
  width: 56px;
  height: 149px;
  padding: 0;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-soft-dark);
  background: var(--color-white);
  overflow: hidden;
  font-size: 0;
  line-height: 0;
  min-height: 0;
}

@media (max-width: 899px) {
  .thumb-btn {
    width: max(48px, min(88px, 28vw));
    height: auto;
    aspect-ratio: 300 / 800;
  }
}

.thumb-btn img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  pointer-events: none;
}

.thumb-btn.selected {
  border-color: var(--color-midnight-blue);
  box-shadow: 0 0 0 1px var(--color-midnight-blue);
}

.canvas-column {
  flex: 1;
  min-width: 0;
  display: grid;
  gap: var(--space-2);
  justify-items: center;
}

canvas {
  width: min(300px, 100%);
  height: auto;
  border-radius: var(--radius-md);
  background: transparent;
  border: 1px solid var(--border-soft-light);
}

.dashboard-grid {
  display: grid;
  grid-template-columns: 1fr;
  column-gap: 12px;
  row-gap: 12px;
  align-items: start;
}

@media (min-width: 480px) {
  .dashboard-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    column-gap: 16px;
    row-gap: 16px;
  }
}

@media (min-width: 900px) {
  .dashboard-grid {
    grid-template-columns: repeat(4, minmax(220px, 1fr));
  }
}

.dashboard-grid .card {
  width: 100%;
  max-width: 230px;
  justify-self: center;
  display: grid;
  gap: var(--space-2);
}

@media (max-width: 479px) {
  .dashboard-grid .card {
    max-width: 100%;
    justify-self: stretch;
  }
}

.thumb {
  width: 100%;
  aspect-ratio: 3 / 8;
  border-radius: var(--radius-md);
  overflow: hidden;
  border: 1px solid var(--border-soft-dark);
  background: var(--color-white);
  display: grid;
  place-items: center;
}

.thumb img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

.meta {
  display: flex;
  justify-content: space-between;
  gap: var(--space-2);
  align-items: baseline;
}

.filename {
  font-size: 12px;
  color: var(--color-olive-green);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: var(--font-mono);
}

.row {
  display: flex;
  gap: var(--space-2);
  flex-wrap: wrap;
}

@media (max-width: 599px) {
  .dashboard-grid .row {
    flex-direction: column;
    align-items: stretch;
  }

  .dashboard-grid .row > button {
    width: 100%;
    justify-content: center;
  }
}

.empty {
  padding: var(--space-4);
  border: 1px dashed var(--border-soft-light);
  border-radius: var(--radius-lg);
  color: var(--text-fine);
}

.empty-detail {
  margin-top: var(--space-2);
}

.auth-page {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: var(--space-6);
  padding-left: max(var(--space-6), env(safe-area-inset-left));
  padding-right: max(var(--space-6), env(safe-area-inset-right));
  padding-bottom: max(var(--space-6), env(safe-area-inset-bottom));
}

.auth-card {
  width: 100%;
  max-width: 380px;
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.links {
  margin-top: 0;
  font-size: 14px;
  line-height: 1.4;
}

.links a {
  color: var(--color-midnight-blue);
}

.debug {
  position: fixed;
  right: var(--space-2);
  bottom: var(--space-2);
  font-size: 12px;
  color: var(--color-white);
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 0;
  padding: var(--space-2) var(--space-3);
  user-select: none;
  pointer-events: none;
}

.auth-copy {
  color: var(--color-olive-green);
  font-size: 14px;
  line-height: 1.4;
  margin: 0 0 var(--space-4);
}

.auth-card > h1 {
  margin: 0;
}

.auth-card .msg {
  margin: 0;
}

.stack-form {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.stack-form .field {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  margin: 0;
}

.stack-form .field label {
  margin-bottom: 0;
}
