/* Mosaic crochet pattern designer — mobile-first */
:root {
  --color-a: #C6EFCE;
  --color-b: #FFFFFF;
  --color-err-vert: #FFB347;
  --color-err-edge: #CC99FF;
  --color-mark-a: #375623;
  --color-mark-b: #555555;
  --bg: #fafafa;
  --fg: #222;
  --panel: #fff;
  --border: #ddd;
  --header-bg: #375623;
  --header-fg: #fff;
  --accent: #375623;
  --danger: #c0392b;
  --shadow: 0 2px 8px rgba(0,0,0,.1);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--fg);
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: env(safe-area-inset-top, 0) 12px 0 12px;
  padding-top: calc(env(safe-area-inset-top, 0) + 8px);
  padding-bottom: 8px;
  background: var(--header-bg);
  color: var(--header-fg);
  box-shadow: var(--shadow);
  z-index: 10;
}
.topbar-left {
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 0;
}
.topbar-left h1 {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.topbar h1 {
  font-size: 17px;
  margin: 0;
  font-weight: 600;
  letter-spacing: .2px;
}
.topbar-right {
  display: flex;
  gap: 8px;
  align-items: center;
}
#lang-select {
  background: rgba(255,255,255,.15);
  color: white;
  border: 1px solid rgba(255,255,255,.3);
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 13px;
}
#lang-select option { color: black; }

.icon-btn {
  background: transparent;
  color: inherit;
  border: none;
  font-size: 22px;
  padding: 6px 10px;
  cursor: pointer;
  border-radius: 6px;
}
.icon-btn:active { background: rgba(255,255,255,.2); }

.menu {
  position: absolute;
  top: calc(env(safe-area-inset-top, 0) + 48px);
  right: 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 6px;
  min-width: 220px;
  z-index: 20;
  display: flex;
  flex-direction: column;
}
.menu button, .menu label {
  background: transparent;
  border: none;
  text-align: left;
  padding: 10px 12px;
  font-size: 15px;
  cursor: pointer;
  border-radius: 6px;
  color: var(--fg);
  display: flex;
  align-items: center;
  gap: 8px;
}
.menu button:hover, .menu label:hover { background: #f0f0f0; }
.menu button.danger { color: var(--danger); }
.menu hr { border: none; border-top: 1px solid var(--border); margin: 4px 0; }
.menu .toggle { font-size: 14px; }
.menu .toggle input { margin: 0; }

main {
  flex: 1;
  overflow: hidden;
  position: relative;
}

.view {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
}
.view[hidden] { display: none; }

.view-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.view-header h2 {
  margin: 0;
  font-size: 16px;
  flex: 1;
}
.back-btn {
  background: transparent;
  border: none;
  font-size: 22px;
  cursor: pointer;
  padding: 4px 10px;
  border-radius: 6px;
}
.back-btn:active { background: #eee; }

.empty-state {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: linear-gradient(180deg, #f5f9f4 0%, #fafafa 280px);
}

.landing {
  max-width: 720px;
  margin: 0 auto;
  padding: 32px 20px 48px;
  display: flex;
  flex-direction: column;
  gap: 32px;
}
.landing-hero {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}
.landing-hero h2 {
  margin: 0;
  font-size: 26px;
  line-height: 1.25;
  color: #2a3a1f;
  font-weight: 700;
  max-width: 520px;
}
.landing-hero p {
  margin: 0;
  font-size: 15px;
  color: #555;
  line-height: 1.5;
  max-width: 520px;
}
.cta-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
  margin-top: 6px;
}
.landing-hero .cta {
  background: var(--accent);
  color: white;
  border: none;
  padding: 14px 26px;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(55, 86, 35, .25);
  transition: transform .1s;
}
.landing-hero .cta.secondary {
  background: white;
  color: var(--accent);
  border: 2px solid var(--accent);
  box-shadow: none;
}
.landing-hero .cta:active { transform: scale(0.97); }

.ios-steps {
  margin: 0 0 14px 0;
  padding-left: 22px;
  font-size: 14.5px;
  line-height: 1.6;
  color: #333;
}
.ios-steps li { margin-bottom: 4px; }
.ios-icon-hint {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: #666;
  background: #f4f7fa;
  padding: 8px 12px;
  border-radius: 6px;
}

.landing-preview {
  display: flex;
  justify-content: center;
  padding: 16px;
  background: white;
  border-radius: 12px;
  box-shadow: var(--shadow);
}
.landing-preview svg {
  width: 100%;
  max-width: 320px;
  height: auto;
  image-rendering: crisp-edges;
}

.landing-features {
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
}
.feature-card {
  background: white;
  border-radius: 10px;
  padding: 16px 18px;
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.feature-icon {
  font-size: 24px;
  line-height: 1;
}
.feature-card h3 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  color: #333;
}
.feature-card p {
  margin: 0;
  font-size: 13.5px;
  color: #666;
  line-height: 1.45;
}

@media (min-width: 600px) {
  .landing-hero h2 { font-size: 30px; }
  .landing-features { grid-template-columns: repeat(3, 1fr); }
}

.grid-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.grid-info {
  padding: 6px 12px;
  font-size: 12px;
  color: #666;
  background: #f4f4f4;
  border-bottom: 1px solid var(--border);
  display: flex;
  gap: 12px;
  justify-content: space-between;
}

.grid-scroll {
  flex: 1;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  background: #eee;
  padding: 8px;
  touch-action: pan-x pan-y;
}

#grid {
  border-collapse: collapse;
  background: white;
  margin: 0 auto;
  user-select: none;
  -webkit-user-select: none;
}
#grid td, #grid th {
  width: 22px;
  height: 22px;
  min-width: 22px;
  text-align: center;
  vertical-align: middle;
  font-size: 11px;
  padding: 0;
  border: 1px solid #ccc;
}
#grid th {
  background: #D9D9D9;
  font-weight: bold;
}
#grid td.cell {
  cursor: pointer;
  font-weight: bold;
  color: #333;
}
#grid td.rownr {
  background: #E6E6E6;
  font-weight: bold;
  min-width: 32px;
  width: 32px;
}
#grid td.clr {
  font-weight: bold;
  font-size: 10px;
}
#grid td.lcol {
  background: white;
}

/* Block borders (every 10 stitches) */
#grid.blocks td.cell.block-top { border-top: 2px solid #000; }
#grid.blocks td.cell.block-bot { border-bottom: 2px solid #000; }
#grid.blocks td.cell.block-lft { border-left: 2px solid #000; }
#grid.blocks td.cell.block-rgt { border-right: 2px solid #000; }

/* Outer border */
#grid.outer td.cell.outer-top { border-top: 3px solid #000; }
#grid.outer td.cell.outer-bot { border-bottom: 3px solid #000; }
#grid.outer td.cell.outer-lft { border-left: 3px solid #000; }
#grid.outer td.cell.outer-rgt { border-right: 3px solid #000; }

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  padding: 8px 12px;
  background: var(--panel);
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: #555;
}
.legend span { display: inline-flex; align-items: center; gap: 4px; }
.legend i {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 2px;
}

.written-content {
  flex: 1;
  overflow: auto;
  padding: 16px;
  background: var(--panel);
}
.written-block {
  margin-bottom: 24px;
}
.written-block h3 {
  margin: 0 0 8px 0;
  padding: 8px 12px;
  background: var(--color-a);
  border-radius: 6px;
  font-size: 14px;
}
.written-block .legend-line {
  font-style: italic;
  color: #666;
  font-size: 13px;
  margin: 0 0 8px 0;
  padding: 0 4px;
}
.written-block ol {
  margin: 0;
  padding-left: 0;
  list-style: none;
}
.written-block li {
  padding: 6px 12px;
  font-size: 14px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  border-bottom: 1px solid #f0f0f0;
}
.written-block li:nth-child(even) { background: #f8f8f8; }

.photo-wrap {
  flex: 1;
  overflow: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #333;
  padding: 16px;
}
#photo-canvas {
  background: white;
  image-rendering: pixelated;
  max-width: 100%;
  height: auto;
}

dialog {
  border: none;
  border-radius: 12px;
  padding: 20px;
  box-shadow: var(--shadow);
  max-width: 90vw;
  width: 320px;
}
dialog::backdrop { background: rgba(0,0,0,.4); }
dialog h3 { margin: 0 0 16px 0; }
dialog label {
  display: block;
  margin-bottom: 12px;
  font-size: 14px;
}
dialog label span { display: block; margin-bottom: 4px; color: #555; }
dialog input {
  width: 100%;
  padding: 10px;
  font-size: 16px;
  border: 1px solid var(--border);
  border-radius: 6px;
}
.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin: 16px 0 0 0;
  padding: 0;
}
.dialog-actions button {
  padding: 10px 16px;
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
}
.dialog-actions button.primary {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.toast {
  position: fixed;
  bottom: calc(env(safe-area-inset-bottom, 0) + 20px);
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,.85);
  color: white;
  padding: 12px 20px;
  border-radius: 24px;
  font-size: 14px;
  z-index: 100;
  max-width: 90vw;
  white-space: pre-line;
  text-align: center;
  box-shadow: var(--shadow);
}
.toast.error { background: var(--danger); }

@media (min-width: 600px) {
  #grid td, #grid th { width: 26px; height: 26px; min-width: 26px; font-size: 13px; }
  #grid td.rownr { min-width: 36px; width: 36px; }
  .topbar h1 { font-size: 19px; }
}
