:root {
  --bg: #f3efe6;
  --bg-deep: #e7e0d2;
  --ink: #14221c;
  --ink-soft: #4a5a52;
  --line: #c9c0ae;
  --panel: #fffdf8;
  --panel-2: #faf6ee;
  --brand: #0b3d2e;
  --brand-2: #14624a;
  --accent: #c45c26;
  --accent-soft: #f3dccf;
  --danger: #a33b2b;
  --shadow: 0 18px 40px rgba(20, 34, 28, 0.08);
  --radius: 18px;
  --mono: "JetBrains Mono", ui-monospace, monospace;
  --sans: "IBM Plex Sans Thai", "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  font-family: var(--sans);
  color: var(--ink);
  background:
    radial-gradient(circle at 12% 8%, rgba(212, 160, 23, 0.18), transparent 28%),
    radial-gradient(circle at 88% 0%, rgba(20, 98, 74, 0.14), transparent 32%),
    linear-gradient(180deg, var(--bg) 0%, var(--bg-deep) 100%);
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

.app-shell {
  max-width: 1280px;
  margin: 0 auto;
  padding: 28px 20px 48px;
}

.topbar, .workspace-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 28px;
  flex-wrap: wrap;
}

.brand-block { display: grid; gap: 6px; }

.brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  font-size: clamp(1.6rem, 3vw, 2.2rem);
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--brand);
}

.brand-mark {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: var(--brand);
  display: grid;
  place-items: center;
}

.brand-mark svg { width: 22px; height: 22px; }

.subtitle {
  margin: 0;
  color: var(--ink-soft);
  font-size: 0.98rem;
}

.btn {
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 0.7rem 1.15rem;
  font-weight: 600;
  transition: transform 0.15s ease, background 0.15s ease;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.btn:hover { transform: translateY(-1px); }
.btn-primary { background: var(--brand); color: #f7f3ea; }
.btn-primary:hover { background: var(--brand-2); }
.btn-secondary { background: var(--panel); border-color: var(--line); }
.btn-ghost { background: transparent; color: var(--brand-2); padding-inline: 0.7rem; }
.btn-danger { background: #f8e8e4; color: var(--danger); border-color: #efc8bf; }
.btn-sm { padding: 0.4rem 0.8rem; font-size: 0.9rem; }

.grid-home {
  display: grid;
  grid-template-columns: 1.1fr 0.9fr;
  gap: 20px;
}

@media (max-width: 960px) {
  .grid-home { grid-template-columns: 1fr; }
}

.panel {
  background: var(--panel);
  border: 1px solid rgba(201, 192, 174, 0.8);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.panel-pad { padding: 22px; }

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.panel-head h2, .section-title {
  margin: 0;
  font-size: 1.15rem;
  color: var(--brand);
}

.muted { color: var(--ink-soft); font-size: 0.92rem; }

.project-list { display: grid; gap: 10px; }

.project-card {
  width: 100%;
  text-align: left;
  border: 1px solid var(--line);
  background: linear-gradient(180deg, #fffefb, #f8f2e7);
  border-radius: 14px;
  padding: 14px 16px;
  display: grid;
  gap: 4px;
}

.project-card:hover { border-color: var(--brand-2); }

.meta-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 16px;
  color: var(--ink-soft);
  font-size: 0.88rem;
}

.empty-state {
  border: 1px dashed var(--line);
  border-radius: 14px;
  padding: 28px 18px;
  text-align: center;
  color: var(--ink-soft);
  background: var(--panel-2);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

@media (max-width: 700px) {
  .form-grid { grid-template-columns: 1fr; }
}

.field { display: grid; gap: 6px; }
.field.full { grid-column: 1 / -1; }
.field label {
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--ink-soft);
}

.field input, .field select, .field textarea, .search-input, .table-input {
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 12px;
  padding: 0.7rem 0.85rem;
  outline: none;
}

.field input:focus, .field select:focus, .field textarea:focus,
.search-input:focus, .table-input:focus {
  border-color: var(--brand-2);
  box-shadow: 0 0 0 3px rgba(20, 98, 74, 0.12);
}

.workspace-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

.cost-mode {
  display: inline-flex;
  background: #efe8da;
  border-radius: 999px;
  padding: 4px;
  gap: 4px;
}

.cost-mode button {
  border: 0;
  background: transparent;
  border-radius: 999px;
  padding: 0.55rem 0.95rem;
  font-weight: 600;
  color: var(--ink-soft);
}

.cost-mode button.active {
  background: var(--brand);
  color: #f7f3ea;
}

.summary-strip {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 18px;
}

@media (max-width: 900px) {
  .summary-strip { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

.summary-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 16px;
  display: grid;
  gap: 6px;
}

.summary-card span { color: var(--ink-soft); font-size: 0.88rem; }
.summary-card strong { font-family: var(--mono); font-size: 1.15rem; }

.summary-card.selected {
  background: linear-gradient(160deg, #134937, #0b3d2e);
  color: #f7f3ea;
  border-color: transparent;
}

.summary-card.selected span { color: rgba(247, 243, 234, 0.75); }

.tabs { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; }

.tab {
  border: 1px solid var(--line);
  background: var(--panel-2);
  border-radius: 999px;
  padding: 0.5rem 1rem;
  font-weight: 600;
}

.tab.active {
  background: var(--accent-soft);
  border-color: #e2b79d;
  color: var(--accent);
}

.attach-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
}

.attach-card {
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
  background: #fff;
  display: grid;
}

.attach-preview {
  aspect-ratio: 16 / 10;
  background: #ece5d7;
  display: grid;
  place-items: center;
  overflow: hidden;
}

.attach-preview img { width: 100%; height: 100%; object-fit: cover; }
.attach-preview iframe { width: 100%; height: 100%; border: 0; }

.file-badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 0.25rem 0.65rem;
  font-size: 0.78rem;
  font-weight: 700;
  background: #e8f2ed;
  color: var(--brand);
}

.file-badge.pdf { background: #f8e8e4; color: var(--danger); }
.file-badge.autocad { background: #e7eef8; color: #24518a; }

.attach-meta { padding: 12px; display: grid; gap: 8px; }
.attach-meta strong { font-size: 0.92rem; overflow-wrap: anywhere; }

.dropzone {
  border: 1.5px dashed var(--line);
  border-radius: 16px;
  padding: 28px 18px;
  text-align: center;
  background: repeating-linear-gradient(
    -45deg,
    rgba(255, 255, 255, 0.35),
    rgba(255, 255, 255, 0.35) 8px,
    rgba(243, 239, 230, 0.55) 8px,
    rgba(243, 239, 230, 0.55) 16px
  );
  margin-bottom: 14px;
}

.dropzone.dragover {
  border-color: var(--brand-2);
  background: rgba(20, 98, 74, 0.06);
}

.table-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 14px;
}

table.boq {
  width: 100%;
  border-collapse: collapse;
  min-width: 1100px;
  background: #fff;
}

table.boq th, table.boq td {
  border-bottom: 1px solid #ebe4d6;
  padding: 8px;
  vertical-align: middle;
  text-align: left;
}

table.boq th {
  background: #f4eee2;
  font-size: 0.82rem;
  color: var(--ink-soft);
  white-space: nowrap;
  position: sticky;
  top: 0;
}

.table-input {
  border: 1px solid transparent;
  background: transparent;
  border-radius: 8px;
  padding: 0.45rem 0.5rem;
}

.table-input:hover { border-color: var(--line); background: #fff; }

.num {
  font-family: var(--mono);
  font-size: 0.88rem;
  text-align: right;
}

.row-actions { display: flex; gap: 4px; flex-wrap: wrap; }

.catalog-list {
  display: grid;
  gap: 8px;
  max-height: 520px;
  overflow: auto;
}

.catalog-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  background: #fff;
}

.catalog-item small { color: var(--ink-soft); }

.toolbar-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.split-2 {
  display: grid;
  grid-template-columns: 1.55fr 0.85fr;
  gap: 16px;
}

@media (max-width: 1000px) {
  .split-2 { grid-template-columns: 1fr; }
}

.hidden-file { display: none; }

.chip {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 0.2rem 0.6rem;
  background: #efe8da;
  color: var(--ink-soft);
  font-size: 0.78rem;
  font-weight: 600;
}

.fade-in { animation: fadeIn 0.35s ease both; }

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

.takeoff-layout {
  display: grid;
  grid-template-columns: 300px 1fr;
  gap: 16px;
  min-height: 560px;
}

@media (max-width: 980px) {
  .takeoff-layout { grid-template-columns: 1fr; }
}

.takeoff-side {
  display: grid;
  gap: 12px;
  align-content: start;
}

.takeoff-side .panel-mini {
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px;
  background: #fff;
  display: grid;
  gap: 8px;
}

.takeoff-side h3 {
  margin: 0;
  font-size: 0.95rem;
  color: var(--brand);
}

.takeoff-stage {
  border: 1px solid var(--line);
  border-radius: 16px;
  background: #1a2420;
  overflow: auto;
  min-height: 520px;
  position: relative;
}

.takeoff-canvas-wrap {
  position: relative;
  display: inline-block;
  margin: 12px;
  cursor: crosshair;
}

.takeoff-canvas-wrap img {
  display: block;
  max-width: none;
  user-select: none;
  -webkit-user-drag: none;
}

.takeoff-canvas-wrap canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.takeoff-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
  align-items: center;
}

.takeoff-stat {
  font-family: var(--mono);
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--brand);
}

.tool-btn.active {
  background: var(--brand);
  color: #f7f3ea;
  border-color: transparent;
}

.btn-accent {
  background: var(--accent);
  color: #fffdf8;
}

.btn-accent:hover {
  filter: brightness(1.05);
}

.ai-box {
  border: 1px solid var(--line);
  border-radius: 16px;
  background: linear-gradient(180deg, #fffefb, #f4eee2);
  padding: 16px;
  display: grid;
  gap: 12px;
}

.ai-progress {
  height: 10px;
  border-radius: 999px;
  background: #e7e0d2;
  overflow: hidden;
}

.ai-progress > span {
  display: block;
  height: 100%;
  background: linear-gradient(90deg, var(--brand), #3dbf8a);
  transition: width 0.25s ease;
}

.ai-progress.is-busy > span {
  background-size: 200% 100%;
  animation: ai-progress-pulse 1.4s linear infinite;
}

@keyframes ai-progress-pulse {
  0% { filter: brightness(1); }
  50% { filter: brightness(1.15); }
  100% { filter: brightness(1); }
}

.ai-status {
  min-height: 1.25em;
  font-size: 0.92rem;
  color: var(--brand-2, #1f6f54);
  font-weight: 600;
}

.draft-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  background: #fff;
}

.draft-table th,
.draft-table td {
  border-bottom: 1px solid #ebe4d6;
  padding: 8px;
  text-align: left;
}

.draft-table th {
  background: #f4eee2;
  color: var(--ink-soft);
  font-size: 0.8rem;
}

.settings-grid {
  display: grid;
  gap: 10px;
}

.log-box {
  max-height: 140px;
  overflow: auto;
  background: #1a2420;
  color: #d7efe4;
  border-radius: 12px;
  padding: 10px 12px;
  font-family: var(--mono);
  font-size: 0.78rem;
  white-space: pre-wrap;
}

.page-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  max-height: 120px;
  overflow: auto;
}

.page-chip {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 999px;
  padding: 0.25rem 0.65rem;
  font-size: 0.82rem;
  font-weight: 600;
}

.page-chip.on {
  background: var(--brand);
  color: #f7f3ea;
  border-color: transparent;
}
