:root {
  --ink: #172d3d;
  --muted: #637989;
  --line: #d7e8f2;
  --soft: #eef8ff;
  --blue: #2787d5;
  --cyan: #19b8ca;
  --green: #16855f;
  --red: #c83f3c;
  --paper: #fff;
  --shadow: 0 12px 30px rgba(23, 45, 61, .1);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Noto Sans TC","Microsoft JhengHei",sans-serif;
  color: var(--ink);
  background: linear-gradient(120deg, rgba(39,135,213,.1), transparent 30%), #f7fbff;
}

body.fullscreen-mode {
  overflow: hidden;
  background: #fff;
}

a { color: var(--blue); text-decoration: none; }
button, input, select, textarea { font: inherit; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 6px 20px;
  align-items: center;
  padding: 8px 5vw;
  background: rgba(255,255,255,.94);
  border-bottom: 1px solid var(--line);
}

.topbar-main {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  align-items: center;
  width: 100%;
}

.topbar-page {
  width: 100%;
  padding-top: 6px;
  border-top: 1px solid var(--line);
}

.topbar-page-inner {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
  width: 100%;
}

.topbar-page-title {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
}

.topbar-page-heading {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.topbar-page h1 {
  margin: 0;
  font-size: 1.05rem;
  line-height: 1.15;
}

.topbar-page .actions {
  margin-left: auto;
}

.brand { display: flex; align-items: center; gap: 10px; color: var(--ink); }
.brand span { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 10px; background: linear-gradient(135deg, var(--blue), var(--cyan)); }
.topbar nav, .subnav, .actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.topbar nav a, .subnav a { padding: 7px 10px; border-radius: 8px; font-weight: 700; }
.topbar nav a.active, .subnav a.active { background: #dff3ff; }
.container { width: min(1240px, 94vw); margin: 25px auto; }
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 14px; margin-bottom: 18px; }
.page-head h1, .card h2, .card h3 { margin: 0; }
.muted { color: var(--muted); }
.grid { display: grid; gap: 16px; }
.grid.cols-2 { grid-template-columns: repeat(2,minmax(0,1fr)); }
.grid.cols-3 { grid-template-columns: repeat(3,minmax(0,1fr)); }
.grid.cols-4 { grid-template-columns: repeat(4,minmax(0,1fr)); }
.card { padding: 18px; border: 1px solid var(--line); border-radius: 12px; background: rgba(255,255,255,.96); box-shadow: var(--shadow); }
.stat { font-size: 2rem; font-weight: 900; color: var(--blue); }
.btn, button { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 0; border-radius: 8px; padding: 10px 14px; background: var(--blue); color: #fff; font-weight: 800; cursor: pointer; }
.btn.secondary, button.secondary { background: #fff; color: var(--ink); border: 1px solid var(--line); }
.btn.green { background: var(--green); }
.btn.danger, button.danger { background: var(--red); }
button.secondary[data-remarks-cache-save] { background: #fff1c7; border-color: #e2c96e; color: #715000; }
button.secondary[data-item-cache-save] { background: #fff1c7; border-color: #e2c96e; color: #715000; }
.button-group { display: inline-flex; align-items: stretch; gap: 0; }
.button-group button { border-radius: 0; border-left-width: 0; }
.button-group button:first-child { border-radius: 8px 0 0 8px; border-left-width: 1px; }
.button-group button:last-child { border-radius: 0 8px 8px 0; }
.button-group .small-delete {
  min-width: 30px;
  min-height: 34px;
  padding: 7px 9px;
  font-size: .9rem;
  line-height: 1;
}
.button-group .small-delete:not(:disabled) { background: var(--red); color: #fff; }
.button-group button:disabled { opacity: .45; cursor: not-allowed; }
.field { display: grid; gap: 6px; }
.field label { font-weight: 800; }
.field-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.field-head > label { margin: 0; }
.remarks-cache-list { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.item-detail-readonly { gap: 12px; }
.item-cache-field { align-content: start; }
.item-cache-field .field-head { align-items: center; }
.item-cache-field .item-cache-save { min-height: 32px; padding: 6px 10px; font-size: .92rem; }
.item-cache-value {
  padding: 10px 12px;
  border: 1px solid rgba(23,45,61,.12);
  border-radius: 8px;
  background: rgba(255,255,255,.72);
  white-space: pre-wrap;
  line-height: 1.55;
  min-height: 46px;
}
.item-cache-list { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.mechanic-field, .category-field { position: relative; }
.mechanic-combobox, .category-combobox { position: relative; display: flex; gap: 8px; align-items: stretch; }
.mechanic-combobox input, .category-combobox input { flex: 1 1 auto; min-width: 0; }
.mechanic-toggle, .category-toggle { flex: 0 0 auto; min-width: 42px; padding: 0 12px; }
.mechanic-dropdown, .category-dropdown {
  position: absolute;
  left: 0;
  right: 0;
  top: calc(100% + 6px);
  z-index: 30;
  display: grid;
  gap: 4px;
  max-height: 220px;
  overflow: auto;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  box-shadow: var(--shadow);
}
.mechanic-option, .category-option {
  width: 100%;
  justify-content: flex-start;
  background: #f8fcff;
  color: var(--ink);
  border: 1px solid var(--line);
}
.input, input, select, textarea { width: 100%; min-height: 42px; border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px; background: #fbfeff; color: var(--ink); }
textarea { min-height: 100px; resize: vertical; }
.form-grid { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 14px; }
.span-2 { grid-column: span 2; }
.table-wrap { overflow: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 11px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table th { color: var(--muted); font-size: .88rem; }
.badge { display: inline-block; padding: 4px 9px; border-radius: 999px; background: var(--soft); font-size: .82rem; font-weight: 800; }
.badge.done { background: #dcf7e8; color: #106343; }
.badge.confirmed { background: #fff1c7; color: #715000; }
.toast {
  position: fixed;
  top: 18px;
  right: 18px;
  z-index: 1000;
  max-width: min(360px, calc(100vw - 36px));
  padding: 12px 14px;
  border-radius: 10px;
  background: #ddf7e9;
  color: #105b3f;
  box-shadow: 0 14px 30px rgba(23, 45, 61, .18);
  transform-origin: top right;
  transition: opacity .18s ease, transform .18s ease, max-height .18s ease, padding .18s ease, margin .18s ease;
}
.toast.delete { background: #ffd6e7; color: #a12d5b; }
.toast.error { background: #ffe5e5; color: #8a2624; }
.toast.is-hiding { opacity: 0; transform: translateY(-8px) scale(.96); max-height: 0; padding-top: 0; padding-bottom: 0; margin: 0; overflow: hidden; }
.alert { margin-bottom: 16px; padding: 12px 14px; border-radius: 8px; background: #ddf7e9; color: #105b3f; }
.alert.error { background: #ffe5e5; color: #8a2624; }
.empty { padding: 30px; text-align: center; color: var(--muted); }
.login { max-width: 440px; margin: 10vh auto; }
.login .brandline { text-align: center; margin-bottom: 20px; }
.fullscreen-shell { position: fixed; inset: 0; background: #fff; }
.app-frame { width: 100%; height: 100%; border: 0; display: block; }
.service-picker { display: grid; grid-template-columns: 280px 1fr; gap: 16px; }
.service-list { max-height: calc(100vh - 112px); overflow: auto; position: sticky; top: 88px; overscroll-behavior: contain; }
.service-tabs-shell {
  width: 100%;
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: 10px 10px 0 0;
  background: #fff;
  padding: 4px 6px 0;
  overflow: hidden;
}
.service-tabs {
  display: flex;
  gap: 2px;
  align-items: end;
  margin: 0;
  overflow-x: auto;
  overflow-y: hidden;
  flex-wrap: nowrap;
  scrollbar-width: none;
  -ms-overflow-style: none;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 2px;
}
.service-tabs::-webkit-scrollbar { display: none; }
.service-tab { display: inline-flex; align-items: center; min-height: 32px; padding: 6px 16px 7px; border: 1px solid var(--line); border-bottom: 0; border-radius: 6px 6px 0 0; background: #eef2f6; color: var(--muted); font-weight: 800; box-shadow: none; position: relative; z-index: 1; }
.service-tab.is-active { background: #dff7e9; color: #0f5f42; z-index: 2; margin-bottom: -1px; }
.service-panels { border: 1px solid var(--line); border-top: 0; border-radius: 0 0 10px 10px; background: #fff; padding: 12px; }
.service-panel { display: grid; gap: 10px; }
.service-group { margin-bottom: 10px; border: 1px solid var(--line); border-radius: 10px; background: #fff; overflow: clip; }
.service-group-head { padding: 12px 14px; background: linear-gradient(180deg, #fff, #f6fbff); font-weight: 900; letter-spacing: .02em; }
.service-group-body { padding: 10px 10px 4px; display: grid; gap: 8px; }
.service-entry { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px; background: #fbfeff; overflow: hidden; }
.service-entry-info { display: grid; gap: 4px; min-width: 0; }
.service-entry form { margin: 0; }
.service-add { width: 38px; min-width: 38px; height: 38px; padding: 0; border-radius: 999px; font-size: 1.1rem; }
.admin-category-tabs { display: flex; flex-wrap: wrap; gap: 6px; margin: 12px 0; }
.admin-category-tab { display: inline-flex; align-items: center; min-height: 34px; padding: 7px 12px; border: 1px solid var(--line); border-radius: 8px; background: #f7fbff; color: var(--ink); font-weight: 800; }
.admin-category-tab.is-active { background: #dff7e9; border-color: rgba(22,133,95,.28); color: #0f5f42; }
.admin-category-group { align-items: stretch; cursor: grab; user-select: none; touch-action: none; }
.admin-category-group.is-dragging { opacity: .6; cursor: grabbing; }
.admin-category-group > * {
  box-sizing: border-box;
  height: 34px;
  min-height: 34px;
  line-height: 1;
}
.admin-category-group .admin-category-tab {
  padding: 0 12px;
  border-right-width: 0;
  border-radius: 8px 0 0 8px;
  cursor: grab;
}
.admin-category-group .admin-category-tab.is-active {
  border-right-width: 0;
}
.admin-category-delete-form { margin: 0; }
.admin-category-group .small-delete {
  padding: 0 9px;
  border-radius: 0 8px 8px 0;
}
.contract-item { margin-top: 10px; padding: 12px 14px; background: var(--soft); border-radius: 10px; }
.contract-item-summary { display: flex; justify-content: space-between; gap: 14px; align-items: center; }
.contract-item-summary-main { min-width: 0; display: flex; align-items: center; gap: 10px 12px; flex-wrap: wrap; }
.contract-item-summary h3 { margin: 0; font-size: 1rem; }
.contract-item-summary-meta { display: flex; align-items: center; gap: 6px 10px; flex-wrap: wrap; color: var(--muted); font-size: .92rem; }
.summary-pill { display: inline-flex; align-items: center; padding: 4px 8px; border-radius: 999px; background: rgba(255,255,255,.7); border: 1px solid rgba(23,45,61,.08); white-space: nowrap; }
.contract-item-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.contract-item-actions form { margin: 0; }
.contract-item-actions button { min-height: 36px; padding: 8px 12px; }
.contract-item-details {
  margin-top: 14px;
  padding: 14px;
  border: 1px solid rgba(22,133,95,.22);
  border-radius: 10px;
  background: #cfeedd;
}
.category-badge { padding: 2px 7px; margin-right: 4px; vertical-align: middle; }
.signature-pad { width: 100%; height: 280px; border: 2px dashed var(--blue); border-radius: 10px; background: #fff; touch-action: none; }
.signature-image { max-width: 100%; height: 150px; object-fit: contain; border: 1px solid var(--line); background: #fff; }
.print-only { display: none; }
.photo-grid { display: grid; grid-template-columns: repeat(3,1fr); gap: 10px; }
.photo-grid img { width: 100%; height: 150px; object-fit: cover; border-radius: 8px; }
.inline-form { display: flex; gap: 8px; align-items: end; }
.subnav { padding: 10px; margin-bottom: 18px; background: #fff; border: 1px solid var(--line); border-radius: 10px; }
[hidden] { display: none !important; }

@media (max-width: 800px) {
  .topbar, .page-head { align-items: flex-start; flex-direction: column; }
  .topbar-main, .topbar-page-inner { align-items: flex-start; flex-direction: column; }
  .topbar-page .actions { margin-left: 0; }
  .topbar-page h1 { font-size: 1rem; }
  .grid.cols-2, .grid.cols-3, .grid.cols-4, .form-grid, .service-picker { grid-template-columns: 1fr; }
  .span-2 { grid-column: auto; }
  .photo-grid { grid-template-columns: 1fr 1fr; }
  .container { margin-top: 16px; }
  .service-list { position: static; max-height: none; }
}

@media print {
  @page { size: A4 portrait; margin: 8mm; }
  * { box-shadow: none !important; }
  html, body { width: 210mm; min-height: 0; background: #fff; color: #000; font-size: 10px; }
  .topbar, .no-print, .subnav, .toast, .alert, .service-list, .contract-item-details { display: none !important; }
  .container { width: 100%; margin: 0; }
  .service-picker { display: block; }
  .service-picker > section { width: 100%; }
  .card { padding: 6px 0; margin-top: 6px !important; border: 0; border-radius: 0; background: #fff; break-inside: avoid; }
  .card h2 { font-size: 15px; margin-bottom: 5px; }
  .contract-items-list { display: grid; gap: 0; }
  .contract-item { margin: 0; padding: 5px 0; border-bottom: 1px solid #ccc; border-radius: 0; background: #fff; break-inside: avoid; }
  .contract-item-summary { display: grid; grid-template-columns: minmax(110px, 1fr) auto; gap: 6px; align-items: center; }
  .contract-item-summary-main { display: grid; grid-template-columns: minmax(100px, 1.2fr) 2fr; gap: 6px; align-items: center; }
  .contract-item-summary h3 { font-size: 11px; }
  .contract-item-summary-meta { display: grid; grid-template-columns: repeat(4, auto); gap: 4px; font-size: 9px; color: #000; }
  .summary-pill { padding: 0; border: 0; background: transparent; white-space: nowrap; }
  .contract-total { margin-top: 6px !important; font-size: 15px; }
  .form-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 4px 10px; }
  .field { gap: 2px; }
  .field label { font-size: 9px; }
  input, select, textarea { min-height: 0; padding: 0; border: 0; border-radius: 0; background: transparent; font-size: 10px; }
  textarea { min-height: 24px; resize: none; }
  .span-2 { grid-column: span 2; }
  .remarks-cache-list { display: none; }
  .item-cache-list { display: none; }
  .signature-pad { height: 120px; }
  .signature-image { height: 70px; }
  .print-only { display: block; }
  .print-contract-head { display: flex; gap: 12px; align-items: center; padding-bottom: 5px; border-bottom: 1px solid #999; font-size: 11px; }
}
