:root {
  --bg: #eeede8;
  --surface: #fbfaf7;
  --surface-2: #f2f0ea;
  --ink: #171a17;
  --muted: #6f756d;
  --line: #d7d3c8;
  --line-strong: #9aa190;
  --cream: #e9e3d5;
  --cream-soft: #f6f3ec;
  --green: #8f9f77;
  --green-dark: #384b42;
  --amber: #b99a57;
  --red: #9d4c43;
  --blue: #5f8180;
  --teal: #5f8180;
  --gold: #b99a57;
  --brown: #6f5a46;
  --brown-2: #9b7b5a;
  --brown-soft: #e7ded2;
  --sidebar: #106766;
  --sidebar-2: #0c5d5d;
  --sidebar-active: #4b8784;
  --sidebar-line: rgba(255, 255, 255, .14);
  --black: #111411;
  --shadow: 0 18px 48px rgba(31, 35, 29, .11);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--ink);
  background: var(--bg);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans Thai", sans-serif;
}

body.locked {
  overflow: hidden;
}

.login-screen {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(circle at 20% 20%, rgba(95, 129, 128, .20), transparent 32%),
    linear-gradient(135deg, #e7e5df, #d5d8ce);
}

.login-screen.hidden {
  display: none;
}

.login-card {
  display: grid;
  gap: 16px;
  width: min(420px, 100%);
  padding: 28px;
  border: 1px solid rgba(255, 255, 255, .55);
  border-radius: 12px;
  background: rgba(251, 250, 247, .94);
  box-shadow: 0 24px 80px rgba(17, 20, 17, .18);
}

.login-logo {
  display: grid;
  place-items: center;
  width: 180px;
  min-height: 72px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
}

.login-logo img {
  display: block;
  width: 156px;
  max-width: 100%;
  height: auto;
  object-fit: contain;
}

.login-card h1 {
  font-size: 32px;
}

.login-card p {
  margin-bottom: 0;
  color: var(--muted);
}

.login-error {
  padding: 10px 12px;
  border: 1px solid rgba(157, 76, 67, .35);
  border-radius: 8px;
  color: var(--red) !important;
  background: rgba(157, 76, 67, .08);
}

button,
input,
textarea,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-columns: 238px 1fr;
  min-height: 100vh;
  transition: grid-template-columns .2s ease;
}

.app-shell.sidebar-collapsed {
  grid-template-columns: 72px 1fr;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  padding: 18px 14px;
  border-right: 1px solid var(--sidebar-line);
  background: linear-gradient(180deg, var(--sidebar), var(--sidebar-2));
  backdrop-filter: blur(18px);
  transition: padding .2s ease;
}

.brand {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: start;
  margin-bottom: 22px;
  position: relative;
  padding-right: 34px;
}

.brand-mark {
  display: grid;
  width: 154px;
  height: 58px;
  place-items: center;
  border-radius: 8px;
  background: #fff;
  border: 1px solid rgba(255, 255, 255, .72);
  overflow: visible;
  padding: 6px 8px;
}

.brand-mark img {
  display: block;
  width: 138px;
  max-width: 100%;
  max-height: 46px;
  height: auto;
  object-fit: contain;
}

.sidebar-toggle {
  display: grid;
  width: 28px;
  height: 28px;
  place-items: center;
  position: absolute;
  right: 0;
  top: 8px;
  border: 1px solid var(--sidebar-line);
  border-radius: 8px;
  color: #fff;
  background: rgba(255, 255, 255, .10);
  z-index: 2;
}

.brand strong {
  color: #f7f3ea;
  font-size: 14px;
  grid-column: 1;
}

.brand span {
  color: rgba(247, 243, 234, .66);
  font-size: 11px;
}

.brand strong,
.brand span {
  display: block;
}

.brand > div:nth-child(2) {
  grid-column: 1;
}

.mini-label,
.eyebrow,
.panel p,
small {
  color: var(--muted);
}

.nav {
  display: grid;
  gap: 4px;
}

.nav-item {
  display: flex;
  width: 100%;
  align-items: center;
  gap: 10px;
  min-height: 40px;
  padding: 0 10px;
  border: 0;
  border-radius: 8px;
  color: rgba(247, 243, 234, .68);
  background: transparent;
  text-align: left;
  font-size: 13px;
}

.nav-item.active,
.nav-item:hover {
  color: #fffaf0;
  background: var(--sidebar-active);
  box-shadow: inset 3px 0 0 rgba(255, 255, 255, .72);
}

.nav-icon {
  display: grid;
  width: 25px;
  height: 25px;
  place-items: center;
  border-radius: 8px;
  background: rgba(255, 255, 255, .08);
  border: 1px solid var(--sidebar-line);
  color: #f7f3ea;
}

.sidebar-card {
  display: grid;
  gap: 4px;
  margin-top: 20px;
  padding: 12px;
  border: 1px solid var(--sidebar-line);
  border-radius: 8px;
  background: rgba(255, 255, 255, .08);
  color: #f7f3ea;
}

.sidebar-card .mini-label,
.sidebar-card small {
  color: rgba(247, 243, 234, .62);
}

.sidebar-actions {
  display: grid;
  gap: 8px;
  margin-top: 18px;
}

.sidebar-action {
  min-height: 36px;
  padding: 0 10px;
  border: 1px solid var(--sidebar-line);
  border-radius: 8px;
  color: #f7f3ea;
  background: rgba(255, 255, 255, .08);
  text-align: left;
}

.sidebar-action:hover {
  background: var(--sidebar-active);
}

.app-shell.sidebar-collapsed .sidebar {
  padding: 18px 10px;
}

.app-shell.sidebar-collapsed .brand {
  justify-content: center;
  padding-right: 0;
}

.app-shell.sidebar-collapsed .brand > div:last-of-type,
.app-shell.sidebar-collapsed .nav-item span:last-child,
.app-shell.sidebar-collapsed .sidebar-actions,
.app-shell.sidebar-collapsed .sidebar-card {
  display: none;
}

.app-shell.sidebar-collapsed .brand-mark {
  width: 50px;
  height: 38px;
  padding: 5px;
}

.app-shell.sidebar-collapsed .brand-mark img {
  width: 42px;
  max-height: 28px;
}

.app-shell.sidebar-collapsed .sidebar-toggle {
  right: 6px;
  top: 62px;
  transform: rotate(180deg);
}

.app-shell.sidebar-collapsed .nav {
  margin-top: 72px;
}

.app-shell.sidebar-collapsed .nav-item {
  justify-content: center;
  padding: 0;
}

.sidebar-card strong {
  font-size: 28px;
}

.main {
  min-width: 0;
  padding: 26px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: center;
  margin-bottom: 22px;
}

.eyebrow {
  margin: 0 0 4px;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: .08em;
  text-transform: uppercase;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(26px, 3vw, 38px);
}

h2 {
  margin-bottom: 4px;
  font-size: 20px;
}

h3 {
  margin-bottom: 0;
  font-size: 16px;
}

.topbar-actions {
  display: flex;
  gap: 10px;
  align-items: center;
}

.search {
  display: flex;
  align-items: center;
  gap: 8px;
  width: min(360px, 36vw);
  min-height: 42px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 1px 0 rgba(29, 23, 19, .03);
}

.search input,
dialog input,
dialog textarea,
dialog select,
dialog datalist {
  width: 100%;
  border: 0;
  outline: 0;
  background: transparent;
}

.primary-button,
.ghost-button,
.icon-button,
.pill-button,
.danger-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 36px;
  border-radius: 8px;
  border: 1px solid transparent;
  white-space: nowrap;
}

.primary-button {
  padding: 0 14px;
  color: #fffaf0;
  background: var(--green-dark);
}

.primary-button:hover {
  background: #26382f;
}

.ghost-button,
.pill-button {
  min-height: 30px;
  padding: 0 9px;
  font-size: 12px;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.button-row.left {
  justify-content: flex-start;
}

.timeline-item .button-row {
  gap: 6px;
}

.timeline-item .pill-button,
.attachment-item .pill-button {
  min-height: 28px;
  padding: 0 8px;
}

.ghost-button {
  padding: 0 12px;
}

.ghost-button,
.pill-button {
  color: var(--ink);
  border-color: var(--line);
  background: var(--surface);
}

.ghost-button:hover,
.pill-button:hover,
.icon-button:hover {
  border-color: var(--line-strong);
  background: var(--cream-soft);
}

.danger-button {
  padding: 0 14px;
  color: #fff;
  border-color: transparent;
  background: var(--red);
}

.icon-button {
  width: 40px;
  padding: 0;
  color: var(--ink);
  border-color: var(--line);
  background: var(--surface);
}

button:disabled {
  cursor: not-allowed;
  opacity: .48;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(160px, 1fr));
  gap: 14px;
  margin-bottom: 14px;
}

.metric {
  min-height: 120px;
  padding: 18px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--surface);
  box-shadow: var(--shadow);
  text-align: left;
  width: 100%;
}

.metric:hover {
  border-color: var(--green);
  transform: translateY(-1px);
}

.metric strong {
  display: block;
  margin: 10px 0 4px;
  font-size: 30px;
}

.content-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.8fr) minmax(300px, .8fr);
  gap: 14px;
}

.panel {
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 1px 0 rgba(255, 255, 255, .8) inset, 0 10px 32px rgba(40, 28, 20, .04);
}

.stacked-panel,
.import-panel {
  margin-bottom: 14px;
}

.import-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(240px, 1fr));
  gap: 12px;
}

.import-box {
  min-height: 86px;
  padding: 14px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.import-box input {
  min-height: auto;
  padding: 0;
  border: 0;
}

.panel-head {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  align-items: start;
  margin-bottom: 14px;
}

.panel p {
  margin-bottom: 0;
  font-size: 13px;
}

.kanban {
  display: grid;
  grid-template-columns: repeat(4, minmax(170px, 1fr));
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
}

.kanban-column {
  min-width: 170px;
  padding: 8px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #e6e5df;
}

.kanban-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  font-weight: 800;
  font-size: 13px;
}

.count {
  display: grid;
  place-items: center;
  min-width: 26px;
  height: 26px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
}

.job-card {
  display: grid;
  gap: 7px;
  padding: 9px;
  margin-bottom: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.job-card:hover {
  box-shadow: var(--shadow);
}

.job-card h3 {
  font-size: 13px;
}

.card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.tag {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 0 8px;
  border-radius: 999px;
  color: #34443b;
  background: #e3e9dc;
  font-size: 12px;
}

.card-actions {
  display: flex;
  gap: 6px;
}

.card-actions button {
  flex: 1;
}

.task-list,
.timeline,
.email-log,
.line-items,
.attachment-list,
.totals-box {
  display: grid;
  gap: 10px;
}

.task,
.timeline-item,
.email-item,
.attachment-item,
.customer-card,
.document-card {
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.task {
  display: grid;
  gap: 6px;
}

.table-wrap {
  overflow-x: auto;
}

.report-filters {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}

.report-filters label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.report-filters input,
.report-filters select {
  min-height: 40px;
  width: 100%;
  min-width: 0;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.report-summary {
  display: grid;
  grid-template-columns: repeat(4, minmax(150px, 1fr));
  gap: 10px;
  margin-bottom: 12px;
}

.report-summary article {
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 8px 24px rgba(40, 28, 20, .04);
}

.report-summary span,
.report-summary strong {
  display: block;
}

.report-summary strong {
  margin-top: 4px;
  font-size: 24px;
}

.report-table-wrap {
  max-height: calc(100vh - 330px);
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  padding: 9px 10px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: middle;
  font-size: 13px;
}

th {
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
}

.table-wrap table {
  min-width: 980px;
}

.report-table-wrap table {
  min-width: 1180px;
}

.line-item,
.timeline-item,
.attachment-item,
.document-card,
.customer-card,
.task,
.email-item {
  font-size: 13px;
}

.timeline-item strong,
.document-card strong {
  font-size: 14px;
}

.document-grid,
.customer-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(220px, 1fr));
  gap: 12px;
}

.document-card,
.customer-card {
  display: grid;
  gap: 8px;
}

.doc-preview {
  display: grid;
  min-height: 110px;
  place-items: center;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  background:
    linear-gradient(var(--surface) 0 0) padding-box,
    repeating-linear-gradient(0deg, transparent, transparent 8px, rgba(106, 146, 144, .10) 9px) border-box;
}

.drawer {
  position: fixed;
  inset: 0;
  display: none;
  z-index: 20;
}

.drawer.open {
  display: block;
}

.drawer-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(20, 25, 22, .28);
}

.drawer-panel {
  position: absolute;
  inset: 0 0 0 auto;
  width: min(760px, 100vw);
  overflow: auto;
  background: var(--bg);
  box-shadow: var(--shadow);
}

.drawer-head {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 22px;
  border-bottom: 1px solid var(--line);
  background: rgba(251, 250, 247, .92);
  backdrop-filter: blur(14px);
}

.drawer-body {
  display: grid;
  gap: 14px;
  padding: 18px;
}

.status-strip {
  display: grid;
  grid-template-columns: repeat(7, minmax(96px, 1fr));
  gap: 6px;
  overflow-x: auto;
}

.stage-step {
  min-height: 54px;
  padding: 8px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}

.stage-step.done {
  color: #fff;
  background: var(--green-dark);
  border-color: var(--green-dark);
}

.stage-step.current {
  color: var(--ink);
  border-color: var(--gold);
  background: #eee6d7;
}

.detail-section {
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 8px 24px rgba(40, 28, 20, .035);
}

.detail-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.info-item {
  padding: 10px;
  border-radius: 8px;
  background: var(--surface-2);
}

.info-item span {
  display: block;
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 12px;
}

.line-item {
  display: grid;
  grid-template-columns: 96px 1fr 72px 110px 110px;
  gap: 10px;
  align-items: center;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.line-item strong,
.line-item small {
  display: block;
}

.line-items.compact .line-item {
  background: var(--surface);
}

.totals-box {
  margin-top: 12px;
  padding: 12px;
  border-radius: 8px;
  background: var(--surface-2);
}

.total-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 0;
}

.total-row.net {
  margin-top: 4px;
  padding-top: 8px;
  border-top: 1px solid var(--line);
  font-weight: 800;
}

.timeline-item,
.email-item,
.attachment-item {
  display: grid;
  gap: 4px;
}

.attachment-item {
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
}

.photo-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(130px, 1fr));
  gap: 10px;
}

.photo {
  overflow: hidden;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--cream-soft);
}

.photo img {
  display: block;
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
}

.photo span {
  display: block;
  padding: 8px;
  color: var(--muted);
  font-size: 12px;
}

.file-button input {
  display: none;
}

dialog {
  width: min(900px, calc(100vw - 28px));
  border: 0;
  padding: 0;
  border-radius: 8px;
  box-shadow: var(--shadow);
}

dialog::backdrop {
  background: rgba(17, 20, 17, .52);
}

.dialog-card {
  display: grid;
  gap: 16px;
  padding: 18px;
  max-height: min(88vh, 920px);
  overflow: auto;
  background: var(--surface);
}

.dialog-head,
.dialog-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.dialog-actions {
  justify-content: flex-end;
}

.dialog-subsection {
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

.button-row.left {
  justify-content: flex-start;
  margin-top: 8px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

label input,
label textarea,
label select {
  min-height: 42px;
  width: 100%;
  min-width: 0;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: #fdfcf8;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

label select {
  appearance: auto;
}

label textarea {
  resize: vertical;
}

.span-2 {
  grid-column: span 2;
}

.doc-type-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.doc-type-button {
  display: grid;
  gap: 6px;
  min-height: 100px;
  padding: 14px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--surface);
  text-align: left;
}

.doc-type-button:hover {
  border-color: var(--green);
}

.doc-confirm {
  display: grid;
  gap: 12px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
}

.doc-summary {
  display: grid;
  gap: 6px;
}

.doc-summary strong {
  font-size: 18px;
}

.choice-list {
  display: grid;
  gap: 8px;
  margin-top: 8px;
}

.check-row {
  display: flex;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
}

.check-row input {
  width: auto;
  min-height: auto;
}

.empty {
  padding: 16px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  background: var(--surface);
}

.status-quote { color: var(--blue); }
.status-po { color: var(--amber); }
.status-delivery { color: var(--teal); }
.status-billing { color: var(--green); }
.status-danger { color: var(--red); }
.status-open { color: var(--green-dark); }
.status-partial { color: var(--gold); }
.status-paid { color: var(--teal); }
.status-overdue { color: var(--red); }

@media (max-width: 1100px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    height: auto;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .nav {
    grid-template-columns: repeat(6, minmax(0, 1fr));
  }

  .nav-item {
    justify-content: center;
  }

  .nav-item span:last-child {
    display: none;
  }

  .sidebar-card {
    display: none;
  }

  .content-grid,
  .metric-grid,
  .document-grid,
  .customer-grid,
  .report-filters {
    grid-template-columns: 1fr 1fr;
  }

  .report-summary {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 740px) {
  .main,
  .sidebar {
    padding: 14px;
  }

  .topbar,
  .topbar-actions,
  .panel-head {
    align-items: stretch;
    flex-direction: column;
  }

  .search {
    width: 100%;
  }

  .content-grid,
  .metric-grid,
  .document-grid,
  .customer-grid,
  .report-filters,
  .report-summary,
  .import-grid,
  .form-grid,
  .info-grid,
  .doc-type-grid,
  .photo-grid {
    grid-template-columns: 1fr;
  }

  .line-item {
    grid-template-columns: 1fr;
  }

  .attachment-item {
    grid-template-columns: 1fr;
  }

  .span-2 {
    grid-column: auto;
  }
}

@media print {
  body {
    background: #fff;
  }

  .sidebar,
  .topbar,
  .drawer,
  dialog {
    display: none !important;
  }

  .main {
    padding: 0;
  }
}
