/* Campong Template Designer — drag & drop A4 canvas */
:root {
  --td-bg: #1a2332;
  --td-panel: #243044;
  --td-accent: #f5c400;
  --td-text: #e8eef5;
  --td-muted: #8fa3bf;
  --td-canvas-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
  --ci-a4-w: 210mm;
  --ci-a4-h: 297mm;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: "Sarabun", "Tahoma", sans-serif;
  background: var(--td-bg);
  color: var(--td-text);
  overflow: hidden;
}

.td-app {
  display: grid;
  grid-template-columns: 220px 1fr minmax(280px, 320px);
  grid-template-rows: 48px 1fr;
  height: 100vh;
  height: 100dvh;
}

.td-topbar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  background: #111a28;
  border-bottom: 1px solid #334;
  flex-wrap: wrap;
  min-height: 48px;
}

.td-topbar h1 {
  margin: 0;
  font-size: 0.95rem;
  flex: 1;
  min-width: 140px;
}

.td-topbar button, .td-topbar select, .td-top-link {
  min-height: 34px;
  padding: 4px 12px;
  border-radius: 6px;
  border: none;
  font: inherit;
  font-size: 0.82rem;
  cursor: pointer;
}

.td-topbar button.primary { background: var(--td-accent); color: #111; font-weight: 700; }
.td-topbar button.ghost, .td-top-link { background: #3a5068; color: #fff; }
.td-top-link { text-decoration: none; display: inline-flex; align-items: center; }
.td-topbar select { background: #2a3a50; color: #fff; min-width: 160px; }

.td-palette, .td-inspector {
  background: var(--td-panel);
  overflow-y: auto;
  padding: 10px;
}

.td-palette h2, .td-inspector h2 {
  margin: 0 0 8px;
  font-size: 0.85rem;
  color: var(--td-accent);
}

.td-subhead { margin-top: 16px !important; }

.td-palette-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  margin-bottom: 6px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px dashed rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  cursor: grab;
  font-size: 0.82rem;
  user-select: none;
}

.td-palette-item:active { cursor: grabbing; }
.td-palette-item .icon { font-size: 1.1rem; width: 1.4rem; text-align: center; }

.td-full-btn {
  width: 100%;
  margin-top: 6px;
  background: #3a5068;
  color: #fff;
  border: none;
  padding: 8px;
  border-radius: 6px;
  cursor: pointer;
  min-height: 36px;
}

.td-canvas-wrap {
  overflow: auto;
  padding: 12px 20px 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: #2a3548;
}

.td-canvas-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  width: 100%;
  max-width: 210mm;
  margin-bottom: 10px;
  padding: 8px;
  background: #1a2332;
  border-radius: 8px;
  align-items: center;
}

.td-menu-group {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
}

.td-menu-label {
  font-size: 0.7rem;
  color: var(--td-muted);
  margin-right: 4px;
  white-space: nowrap;
}

.td-tool-btn {
  min-width: 36px;
  min-height: 36px;
  padding: 4px 8px;
  border: 1px solid #456;
  border-radius: 6px;
  background: #2a3a50;
  color: #fff;
  font: inherit;
  font-size: 0.75rem;
  cursor: pointer;
}

.td-tool-btn:hover, .td-tool-btn.active {
  background: var(--td-accent);
  color: #111;
  border-color: var(--td-accent);
}

.td-tool-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.td-btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.td-btn-row .td-tool-btn { flex: 1; min-width: 44px; }

.td-check {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.75rem;
  color: var(--td-text);
  min-height: 36px;
  padding: 0 6px;
}

.td-check-block { display: flex; margin-bottom: 8px; }
.td-check input { width: 18px; height: 18px; accent-color: var(--td-accent); }

.td-canvas {
  width: var(--ci-a4-w);
  height: var(--ci-a4-h);
  background: #fff;
  color: #111;
  position: relative;
  box-shadow: var(--td-canvas-shadow);
  flex-shrink: 0;
  overflow: hidden;
  user-select: none;
  touch-action: none;
}

.td-canvas.drag-over { outline: 3px dashed var(--td-accent); outline-offset: 4px; }

.td-guide-v, .td-guide-h {
  position: absolute;
  pointer-events: none;
  z-index: 20000;
  background: #e91e63;
}

.td-guide-v { top: 0; bottom: 0; width: 1px; }
.td-guide-h { left: 0; right: 0; height: 1px; }

.td-hint {
  width: 100%;
  max-width: 210mm;
  margin: 0 0 8px;
  font-size: 0.72rem;
  color: var(--td-muted);
  line-height: 1.4;
}

.td-inspector .td-hint { max-width: none; }

.td-marquee, .td-sel-box {
  position: absolute;
  pointer-events: none;
  z-index: 18000;
  box-sizing: border-box;
}

.td-marquee {
  border: 1px dashed #1976d2;
  background: rgba(25, 118, 210, 0.12);
}

.td-sel-box {
  border: 2px solid #f5c400;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.2);
}

.td-sel-box.has-group::after {
  content: "กลุ่ม";
  position: absolute;
  top: -18px;
  left: 0;
  font-size: 10px;
  line-height: 16px;
  padding: 0 6px;
  background: #f5c400;
  color: #111;
  border-radius: 3px;
  font-family: Tahoma, sans-serif;
}

.td-el {
  position: absolute;
  border: 1px dashed transparent;
  cursor: move;
  overflow: visible;
  min-width: 8px;
  min-height: 8px;
  touch-action: none;
}

.td-el.locked { cursor: default; }

.td-el.selected {
  outline: 2px solid #1976d2;
  outline-offset: 1px;
}

.td-el.selected.grouped {
  outline-color: #f5c400;
}

.td-el .td-el-inner {
  width: 100%;
  height: 100%;
  pointer-events: none;
  font-family: Tahoma, Sarabun, sans-serif;
  white-space: pre-wrap;
  word-break: break-word;
  overflow: hidden;
}

.td-el.type-rect .td-el-inner { border: 1px solid #000; }
.td-el.type-line .td-el-inner { border-top: 2px solid #000; margin-top: 50%; }
.td-el.type-table .td-el-inner {
  border: none;
  background: #fff;
  display: block;
  overflow: hidden;
  color: #111;
}

.td-el.type-table table {
  width: 100%;
  height: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  font-size: 6pt;
}

.td-el.type-table th,
.td-el.type-table td {
  border: 1px solid #000;
  padding: 1px 2px;
  overflow: hidden;
  word-break: break-word;
}

.td-el.type-table th {
  background: #6d6d6d;
  color: #fff;
  font-weight: 700;
  text-align: center;
}

.td-el.type-table .td-el-inner-ph {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #666;
  font-size: 8pt;
  border: 1px dashed #999;
}

.td-el.type-image .td-el-inner {
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
}

.td-el.type-variable .td-el-inner {
  background: rgba(245, 196, 0, 0.15);
  border: 1px dotted #b8860b;
  font-size: 7pt;
  padding: 2px 4px;
}

.td-resize {
  position: absolute;
  width: 10px;
  height: 10px;
  background: #1976d2;
  border: 1px solid #fff;
  display: none;
  z-index: 10000;
  pointer-events: auto;
}

.td-el.selected.primary .td-resize { display: block; }

.td-resize.n { top: -5px; left: 50%; margin-left: -5px; cursor: n-resize; }
.td-resize.s { bottom: -5px; left: 50%; margin-left: -5px; cursor: s-resize; }
.td-resize.e { right: -5px; top: 50%; margin-top: -5px; cursor: e-resize; }
.td-resize.w { left: -5px; top: 50%; margin-top: -5px; cursor: w-resize; }
.td-resize.ne { top: -5px; right: -5px; cursor: ne-resize; }
.td-resize.nw { top: -5px; left: -5px; cursor: nw-resize; }
.td-resize.se { bottom: -5px; right: -5px; cursor: se-resize; }
.td-resize.sw { bottom: -5px; left: -5px; cursor: sw-resize; }

.td-field { margin-bottom: 8px; }
.td-field label {
  display: block;
  font-size: 0.72rem;
  color: var(--td-muted);
  margin-bottom: 2px;
}

.td-field input,
.td-field select,
.td-field textarea {
  width: 100%;
  padding: 6px 8px;
  border: 1px solid #456;
  border-radius: 6px;
  background: #1a2332;
  color: #fff;
  font: inherit;
  font-size: 0.82rem;
}

.td-field input[type="color"] { min-height: 36px; padding: 2px; }
.td-field input[type="range"] { padding: 0; min-height: 28px; accent-color: var(--td-accent); }
.td-field input[type="file"] { font-size: 0.72rem; }
.td-field textarea { min-height: 64px; resize: vertical; }

.td-range-val {
  display: inline-block;
  font-size: 0.7rem;
  color: var(--td-muted);
  margin-top: 2px;
}

.td-section {
  margin: 0 0 10px;
  padding: 6px 8px 8px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.15);
}

.td-section summary {
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--td-accent);
  padding: 6px 0;
  min-height: 36px;
  display: flex;
  align-items: center;
}

.td-var-list, .td-layer-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 160px;
  overflow-y: auto;
  font-size: 0.75rem;
}

.td-var-list li, .td-layer-list li {
  padding: 6px 8px;
  margin-bottom: 3px;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 4px;
  cursor: pointer;
  min-height: 32px;
}

.td-var-list li:hover, .td-layer-list li:hover { background: rgba(245, 196, 0, 0.2); }
.td-var-list code { color: var(--td-accent); }
.td-layer-list li.active { outline: 1px solid var(--td-accent); }
.td-layer-list li.in-group { border-left: 3px solid var(--td-accent); }
.td-layer-list .z { color: var(--td-muted); float: right; }

.td-status {
  font-size: 0.72rem;
  color: var(--td-muted);
  padding: 6px 0;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  margin-top: 8px;
}

.td-status.ok { color: #6fcf97; }
.td-status.err { color: #ff8a80; }

.td-row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }

.td-step-menu {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 10px;
}

.td-step {
  display: grid;
  grid-template-columns: 1fr 36px minmax(52px, 1fr) 36px;
  align-items: center;
  gap: 4px;
}

.td-step > span:first-child {
  font-size: 0.72rem;
  color: var(--td-muted);
}

.td-step input,
.td-step-val {
  width: 100%;
  text-align: center;
  padding: 6px 4px;
  min-height: 36px;
  border: 1px solid #456;
  border-radius: 6px;
  background: #1a2332;
  color: #fff;
  font: inherit;
  font-size: 0.82rem;
}

.td-step-val {
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
}

.td-table-row-actions { margin-bottom: 8px; }

.td-mini-grid .td-grid-tools {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 2px;
}

.td-mini-grid .td-grid-tools button,
.td-mini-grid .td-row-tools button {
  min-width: 28px;
  min-height: 28px;
  padding: 0;
  font-size: 0.85rem;
  line-height: 1;
}

.td-mini-grid .td-row-tools {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 2px;
}

.td-mini-grid {
  overflow-x: auto;
  max-height: 220px;
  overflow-y: auto;
  border: 1px solid #456;
  border-radius: 6px;
  background: #1a2332;
}

.td-mini-grid table {
  border-collapse: collapse;
  width: max-content;
  min-width: 100%;
}

.td-mini-grid th,
.td-mini-grid td {
  padding: 0;
  border: 1px solid #456;
}

.td-mini-grid .td-grid-label {
  font-size: 0.65rem;
  color: var(--td-muted);
  padding: 2px 4px;
  min-width: 28px;
  text-align: center;
}

.td-mini-grid input {
  min-width: 72px;
  width: 100%;
  border: none;
  border-radius: 0;
  padding: 6px 4px;
  min-height: 36px;
}

.td-mini-grid thead input { background: #3a5068; font-weight: 700; }

.td-danger {
  width: 100%;
  margin-top: 8px;
  padding: 10px;
  background: #c0392b;
  color: #fff;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  min-height: 44px;
  font: inherit;
}

@media (max-width: 1100px) {
  html, body { overflow: auto; }
  .td-app {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto 1fr auto;
    height: auto;
    min-height: 100dvh;
  }
  .td-topbar { position: sticky; top: 0; z-index: 30; }
  .td-palette, .td-inspector { max-height: 42vh; }
  .td-tool-btn { min-width: 44px; min-height: 44px; }
  .td-mini-grid input { min-height: 44px; }
  .td-step { grid-template-columns: 1fr 44px minmax(52px, 1fr) 44px; }
  .td-step input, .td-step-val { min-height: 44px; }
  .td-mini-grid .td-grid-tools button,
  .td-mini-grid .td-row-tools button { min-width: 44px; min-height: 36px; }
  .td-canvas-wrap { padding: 10px; }
}

@media (max-width: 600px) {
  .td-topbar h1 { font-size: 0.8rem; }
  .td-row2 { grid-template-columns: 1fr; }
  .td-canvas {
    width: min(100%, 210mm);
    height: calc(min(100%, 210mm) * 297 / 210);
  }
}

@media print {
  .td-topbar, .td-palette, .td-inspector, .td-canvas-toolbar { display: none !important; }
  .td-canvas-wrap { padding: 0; background: #fff; }
  .td-el { border: none !important; outline: none !important; }
  .td-resize, .td-guide-v, .td-guide-h { display: none !important; }
}
