:root {
  --color-bg: #f1f5f9;
  --color-sidebar: #ffffff;
  --color-border: #e2e8f0;
  --color-text: #1e293b;
  --color-text-dim: #64748b;
  --color-primary: #2563eb;
  --color-primary-dark: #1d4ed8;
  --color-danger: #dc2626;
  --color-ok: #16a34a;
  --color-warn: #d97706;
  --radius: 8px;
  --sidebar-width: 340px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: 'Sarabun', 'Segoe UI', sans-serif;
  color: var(--color-text);
  background: var(--color-bg);
}

.app {
  display: flex;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--color-sidebar);
  border-right: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  height: 100%;
}

.sidebar-header {
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--color-border);
}

.sidebar-header h1 {
  font-size: 17px;
  margin: 0;
  font-weight: 700;
  color: var(--color-primary-dark);
}

.subtitle {
  margin: 2px 0 0;
  font-size: 12.5px;
  color: var(--color-text-dim);
}

.sidebar-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 10px 16px 30px;
}

.panel {
  border-bottom: 1px solid var(--color-border);
  padding: 14px 0;
}

.panel:last-child { border-bottom: none; }

.panel-title {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .03em;
  color: var(--color-text-dim);
  margin: 0 0 10px;
}

/* Dropzone */
.dropzone {
  border: 2px dashed var(--color-border);
  border-radius: var(--radius);
  padding: 16px 12px;
  text-align: center;
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.dropzone:hover, .dropzone.dragover {
  border-color: var(--color-primary);
  background: #eff6ff;
}
.dropzone p { margin: 0 0 6px; font-size: 13.5px; }
.file-name { font-size: 12px; color: var(--color-text-dim); word-break: break-all; }

/* Forms */
.form-row { display: flex; gap: 8px; margin-bottom: 8px; }
.form-row-3 .field { flex: 1; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; }
.field label { font-size: 12px; color: var(--color-text-dim); }
.field input {
  border: 1px solid var(--color-border);
  border-radius: 6px;
  padding: 7px 8px;
  font-size: 13.5px;
  font-family: inherit;
  width: 100%;
}
.form-row-3 input { padding: 7px 6px; }
.field input:focus { outline: 2px solid var(--color-primary); outline-offset: -1px; }

.info-line {
  font-size: 12.5px;
  color: var(--color-text-dim);
  margin-top: 6px;
}
.info-line strong { color: var(--color-text); }

.hint {
  font-size: 11.5px;
  color: var(--color-text-dim);
  line-height: 1.5;
  margin: 8px 0 0;
}

/* Buttons */
.btn {
  border: 1px solid var(--color-border);
  background: #fff;
  color: var(--color-text);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  transition: background .12s, border-color .12s, color .12s;
  text-align: center;
}
.btn:hover:not(:disabled) { background: #f8fafc; border-color: #cbd5e1; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-block { width: 100%; margin-bottom: 8px; }

.btn-primary {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: #fff;
}
.btn-primary:hover:not(:disabled) { background: var(--color-primary-dark); }

.btn-secondary { background: #fff; }

.btn-danger-outline {
  background: #fff;
  border-color: #fecaca;
  color: var(--color-danger);
}
.btn-danger-outline:hover:not(:disabled) { background: #fef2f2; }

.btn-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.btn-grid .btn { width: 100%; }

.btn.active {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: #fff;
}

/* Results */
.result-grid { display: flex; flex-direction: column; gap: 6px; }
.result-row {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  padding: 4px 0;
  border-bottom: 1px dashed var(--color-border);
}
.result-row span:first-child { color: var(--color-text-dim); }
.result-row span:last-child { font-weight: 600; }
.result-value.ok { color: var(--color-ok); }
.result-value.warn { color: var(--color-warn); }

/* Main / canvas area */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100%;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 14px;
  background: #fff;
  border-bottom: 1px solid var(--color-border);
  gap: 12px;
  flex-wrap: wrap;
}

.toolbar-group { display: flex; align-items: center; gap: 6px; }

.btn-icon {
  border: 1px solid var(--color-border);
  background: #fff;
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 13px;
  cursor: pointer;
  font-family: inherit;
}
.btn-icon:hover { background: #f8fafc; }
.btn-icon.active { background: var(--color-primary); color: #fff; border-color: var(--color-primary); }

.zoom-label {
  font-size: 12.5px;
  color: var(--color-text-dim);
  min-width: 42px;
  text-align: center;
}

.canvas-wrapper {
  flex: 1;
  position: relative;
  overflow: hidden;
  background-color: #dbe2ea;
  background-image:
    linear-gradient(45deg, #cfd8e3 25%, transparent 25%),
    linear-gradient(-45deg, #cfd8e3 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #cfd8e3 75%),
    linear-gradient(-45deg, transparent 75%, #cfd8e3 75%);
  background-size: 24px 24px;
  background-position: 0 0, 0 12px, 12px -12px, -12px 0px;
}

#mainCanvas {
  position: absolute;
  top: 0;
  left: 0;
  display: block;
  cursor: crosshair;
}

.status-bar {
  padding: 6px 14px;
  font-size: 12.5px;
  color: var(--color-text-dim);
  background: #fff;
  border-top: 1px solid var(--color-border);
}

/* Crop overlay (PDF export) */
.crop-overlay {
  position: absolute;
  inset: 0;
  display: none;
  z-index: 20;
}
.crop-overlay.active { display: block; }

.crop-mask {
  position: absolute;
  background: rgba(15, 23, 42, .6);
}

.crop-box {
  position: absolute;
  border: 2px dashed #fff;
  box-shadow: 0 0 0 1px rgba(37,99,235,.9);
  cursor: move;
}

.crop-handle {
  position: absolute;
  width: 14px;
  height: 14px;
  background: #fff;
  border: 2px solid var(--color-primary);
  border-radius: 3px;
}
.crop-handle[data-handle="nw"] { top: -8px; left: -8px; cursor: nwse-resize; }
.crop-handle[data-handle="ne"] { top: -8px; right: -8px; cursor: nesw-resize; }
.crop-handle[data-handle="sw"] { bottom: -8px; left: -8px; cursor: nesw-resize; }
.crop-handle[data-handle="se"] { bottom: -8px; right: -8px; cursor: nwse-resize; }

.crop-toolbar {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 23, 42, .88);
  color: #fff;
  padding: 8px 12px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 12.5px;
  max-width: calc(100% - 24px);
  flex-wrap: wrap;
  box-shadow: 0 4px 14px rgba(0,0,0,.25);
}
.crop-toolbar-actions { display: flex; gap: 8px; }
.crop-toolbar .btn { padding: 6px 10px; font-size: 12.5px; border: none; }
.crop-toolbar .btn-secondary { background: #334155; color: #fff; }
.crop-toolbar .btn-secondary:hover { background: #475569; }

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, .45);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal-overlay.active { display: flex; }

.modal-box {
  background: #fff;
  border-radius: 10px;
  padding: 20px 22px;
  width: 340px;
  max-width: 90vw;
  box-shadow: 0 10px 40px rgba(0,0,0,.25);
}
.modal-box h3 { margin: 0 0 12px; font-size: 15.5px; }
.modal-box p { margin: 0 0 14px; font-size: 13.5px; color: var(--color-text-dim); line-height: 1.5; }
.modal-box label { display: block; font-size: 12.5px; color: var(--color-text-dim); margin-bottom: 6px; }
.modal-box input {
  width: 100%;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 14px;
  font-family: inherit;
  margin-bottom: 6px;
}
.modal-box input:focus { outline: 2px solid var(--color-primary); outline-offset: -1px; }
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 16px;
}
.modal-actions .btn { padding: 8px 16px; }

/* Scrollbar */
.sidebar-scroll::-webkit-scrollbar { width: 8px; }
.sidebar-scroll::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }
