:root {
  --dark-brand: #13384F;
  --brand-blue: #1F5E8C;
  --brand-blue-2: #2C7CB6;
  --primary-green: #2F9E76;
  --cool-gray: #EFF3F5;
  --canvas-gray: #E4E9EC;
  --line: #E0E7EB;
  --line-2: #E4EBEF;
  --text-dark: #16222E;
  --text-secondary: #5B6B78;
  --text-muted: #8B98A2;
  --warning-accent: #FFD98A;
  --light-cyan: #9FD2EA;
  --success-bg: #E2F4ED;
  --info-bg: #E4F0F7;
  --warn-bg: #FBF1DE;
  --danger-bg: #F7E7E4;
  --card: #FFFFFF;
  --shadow: 0 18px 46px -28px rgba(19, 56, 79, .55);
  --shadow-soft: 0 10px 28px -22px rgba(19, 56, 79, .45);
  --radius: 14px;
}

* { box-sizing: border-box; }

html { background: var(--dark-brand); }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--canvas-gray);
  color: var(--text-dark);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  line-height: 1.5;
  display: grid;
  grid-template-columns: 248px minmax(0, 1fr);
}

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

button {
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
  color: var(--text-dark);
  padding: 9px 13px;
  font-weight: 800;
  cursor: pointer;
}

button:hover { border-color: var(--brand-blue); color: var(--brand-blue); }

.primary {
  background: var(--primary-green);
  border-color: var(--primary-green);
  color: #fff;
  box-shadow: 0 10px 20px -14px rgba(47, 158, 118, .8);
}

.primary:hover {
  color: #fff;
  background: #287F60;
}

.app-shell {}

.auth-pending .side,
.auth-pending .content,
.auth-required .side,
.auth-required .content,
.auth-change-required .side,
.auth-change-required .content { display: none; }

.auth-required,
.auth-change-required {
  display: grid;
  grid-template-columns: 1fr;
  place-items: center;
  min-height: 100vh;
  padding: 22px;
  background:
    radial-gradient(circle at 18% 8%, rgba(159, 210, 234, .3), transparent 34%),
    linear-gradient(150deg, var(--dark-brand), var(--brand-blue));
}

.login-screen { width: min(100%, 460px); }

.login-card {
  display: grid;
  gap: 14px;
  background: var(--card);
  border: 1px solid rgba(255,255,255,.22);
  border-radius: 22px;
  padding: 24px;
  box-shadow: 0 26px 70px -34px rgba(0,0,0,.55);
}

.login-card h1 {
  margin: 0;
  color: var(--dark-brand);
  font-size: 28px;
  line-height: 1.2;
}

.login-card p { margin: 0; color: var(--text-secondary); }
.login-message { min-height: 24px; color: var(--brand-blue); font-weight: 900; }

.top-actions { display: flex; align-items: center; gap: 10px; }
.top-actions button { background: rgba(255,255,255,.14); color: #fff; border-color: rgba(255,255,255,.24); }

.side {
  position: sticky;
  top: 0;
  height: 100vh;
  padding: 18px 12px;
  color: #fff;
  background:
    radial-gradient(circle at 20% 0%, rgba(159, 210, 234, .18), transparent 32%),
    linear-gradient(180deg, var(--dark-brand), #102B3D 64%, #0D2231);
}

.brand {
  padding: 10px 10px 20px;
  border-bottom: 1px solid rgba(255,255,255,.12);
  margin-bottom: 12px;
}

.brand small {
  display: block;
  color: var(--light-cyan);
  font-size: 11px;
  font-weight: 900;
  letter-spacing: .18em;
  text-transform: uppercase;
}

.brand strong {
  display: block;
  margin-top: 6px;
  font-size: 21px;
  line-height: 1.25;
}

.nav {
  width: 100%;
  margin: 4px 0;
  text-align: left;
  background: transparent;
  border: 1px solid transparent;
  color: #DCEAF1;
  border-radius: 12px;
}

.nav.active,
.nav:hover {
  background: rgba(255,255,255,.12);
  border-color: rgba(255,255,255,.14);
  color: #fff;
}

.content {
  min-width: 0;
  background: var(--cool-gray);
}

.top {
  position: sticky;
  top: 0;
  z-index: 4;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 24px;
  background: linear-gradient(135deg, var(--dark-brand), var(--brand-blue) 62%, var(--brand-blue-2));
  color: #fff;
  box-shadow: var(--shadow);
}

.top h1 {
  margin: 0;
  font-size: clamp(22px, 3vw, 31px);
  line-height: 1.15;
}

.top p {
  margin: 7px 0 0;
  color: #CFE6F2;
  font-size: 13px;
}

.pill {
  border: 1px solid rgba(255,255,255,.24);
  background: rgba(255,255,255,.12);
  color: #fff;
  border-radius: 999px;
  padding: 7px 12px;
  font-weight: 900;
  white-space: nowrap;
}

.screen {
  display: none;
  padding: 22px;
}

.screen.active { display: block; }

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

.screen-title h2 {
  margin: 0;
  color: var(--text-dark);
  font-size: clamp(24px, 3vw, 31px);
}

.screen-title p {
  margin: 5px 0 0;
  color: var(--text-secondary);
}

.card,
.metric-card {
  background: var(--card);
  border: 1px solid var(--line-2);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow-soft);
  margin-bottom: 14px;
}

.card h3 {
  margin: 0 0 11px;
  color: var(--brand-blue);
  font-size: 17px;
}

.grid { display: grid; gap: 14px; }
.two { grid-template-columns: minmax(300px, 430px) 1fr; }
.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }

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

.wide { grid-column: 1 / -1; }

label {
  display: grid;
  gap: 6px;
  color: var(--brand-blue);
  font-weight: 900;
}

input,
select,
textarea {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 9px 11px;
  background: #fff;
  color: var(--text-dark);
  font-weight: 400;
}

textarea { min-height: 84px; resize: vertical; }

input:focus,
select:focus,
textarea:focus {
  outline: 3px solid rgba(31, 94, 140, .14);
  border-color: var(--brand-blue);
}

fieldset {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
}

legend {
  color: var(--brand-blue);
  font-weight: 700;
  padding: 0 7px;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 9px;
}

.list,
.cards {
  display: grid;
  gap: 10px;
}

.row {
  border: 1px solid var(--line-2);
  border-radius: 13px;
  background: #fff;
  padding: 12px;
  cursor: pointer;
  transition: transform .15s ease, border-color .15s ease, box-shadow .15s ease;
}

.row:hover { transform: translateY(-1px); }

.row.active {
  border-color: var(--brand-blue);
  box-shadow: 0 0 0 4px rgba(31, 94, 140, .12);
}

.row strong {
  display: block;
  color: var(--text-dark);
}

.meta {
  color: var(--text-secondary);
  font-size: 13px;
}

.kv {
  display: grid;
  grid-template-columns: 150px 1fr;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid #EEF3F5;
}

.kv:last-child { border-bottom: 0; }
.kv span:first-child { color: var(--text-secondary); }

.kv strong,
.mono {
  font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, Consolas, monospace;
}

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

.checks label {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  border: 1px solid var(--line-2);
  border-radius: 11px;
  padding: 8px;
  color: var(--text-dark);
  font-weight: 750;
  background: #fff;
}

.checks input {
  width: auto;
  min-height: auto;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 9px;
  min-height: 44px;
  border: 1px solid var(--line-2);
  border-radius: 11px;
  padding: 9px 10px;
  background: #fff;
}

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

.total {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.total div {
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 10px;
  background: var(--info-bg);
}

.total strong {
  display: block;
  font-size: 20px;
  color: var(--brand-blue);
}

.status-chip,
.tag {
  display: inline-flex;
  align-items: center;
  min-height: 26px;
  border-radius: 999px;
  padding: 3px 9px;
  font-size: 12px;
  font-weight: 900;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text-secondary);
}

.status-chip.info,
.tag.info {
  color: var(--brand-blue);
  background: var(--info-bg);
  border-color: #BFDCEC;
}

.status-chip.safe,
.tag.safe {
  color: var(--primary-green);
  background: var(--success-bg);
  border-color: #B6E2D2;
}

.status-chip.warn,
.tag.warn {
  color: #9A650D;
  background: var(--warn-bg);
  border-color: #EFD9AE;
}

.status-chip.danger,
.tag.danger {
  color: #A04B37;
  background: var(--danger-bg);
  border-color: #E5C7BF;
}

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

.metric-card {
  margin: 0;
  padding: 13px;
}

.metric-card span {
  display: block;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 800;
}

.metric-card strong {
  display: block;
  margin-top: 3px;
  color: var(--brand-blue);
  font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 24px;
}

.section-label {
  color: var(--light-cyan);
  font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 11px;
  font-weight: 900;
  letter-spacing: .16em;
  text-transform: uppercase;
}

@media (max-width: 900px) {
  body {
    display: block;
    padding-bottom: 82px;
    background: var(--cool-gray);
  }

  .side {
    position: fixed;
    left: 10px;
    right: 10px;
    bottom: 10px;
    top: auto;
    z-index: 20;
    height: 64px;
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    align-items: center;
    gap: 6px;
    padding: 7px;
    border-radius: 24px;
    box-shadow: 0 18px 40px -20px rgba(19, 56, 79, .65);
  }

  .brand { display: none; }

  .nav {
    min-height: 50px;
    margin: 0;
    padding: 6px 4px;
    text-align: center;
    font-size: 11px;
    border-radius: 16px;
  }

  .nav[data-tab="cases"] {
    background: var(--primary-green);
    color: #fff;
    box-shadow: 0 10px 20px -12px rgba(47, 158, 118, .8);
  }

  .nav[data-tab="cases"].active {
    border-color: rgba(255,255,255,.2);
  }

  .top {
    position: static;
    display: block;
    padding: 18px 16px;
  }

  .top .pill {
    display: inline-flex;
    margin-top: 12px;
  }

  .screen { padding: 16px; }

  .screen-title {
    display: block;
  }

  .screen-title .actions { margin-top: 12px; }

  .two,
  .three,
  .form-grid,
  .metric-strip {
    grid-template-columns: 1fr;
  }

  .kv { grid-template-columns: 112px 1fr; }
  .checks { grid-template-columns: 1fr; }
}

.mobile-bottom-nav,
.action-sheet,
.sheet-backdrop { display: none; }

.nav span,
.nav small { display: block; }
.nav small {
  margin-top: 2px;
  color: rgba(220, 234, 241, .74);
  font-size: 11px;
  font-weight: 750;
}
.nav.active small,
.nav:hover small { color: rgba(255,255,255,.82); }

.metric-card small {
  display: block;
  margin-top: 2px;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 800;
}

.empty-state {
  display: grid;
  gap: 4px;
  border: 1px dashed #BFDCEC;
  border-radius: var(--radius);
  padding: 14px;
  color: var(--text-secondary);
  background: linear-gradient(135deg, #fff, var(--info-bg));
}

.empty-state strong { color: var(--brand-blue); }
.dashboard-empty { margin: -2px 0 14px; }

@media (max-width: 768px) {
  html { background: var(--dark-brand); }

  body.authenticated {
    display: block;
    min-height: 100vh;
    padding: 0 0 92px;
    background: var(--dark-brand);
  }

  body.authenticated::before {
    content: "";
    position: fixed;
    inset: 0;
    pointer-events: none;
    background:
      radial-gradient(circle at 14% 0%, rgba(159, 210, 234, .28), transparent 32%),
      linear-gradient(180deg, var(--dark-brand) 0 210px, var(--cool-gray) 210px 100%);
  }

  .side { display: none; }

  .content {
    position: relative;
    z-index: 1;
    width: min(100%, 480px);
    min-height: 100vh;
    margin: 0 auto;
    overflow: hidden;
    background: var(--cool-gray);
    border-radius: 0 0 28px 28px;
    box-shadow: 0 28px 70px -46px rgba(0,0,0,.55);
  }

  .top {
    position: sticky;
    top: 0;
    z-index: 8;
    display: flex;
    align-items: flex-start;
    padding: 15px 16px 13px;
    border-radius: 0 0 24px 24px;
    box-shadow: 0 18px 38px -26px rgba(19, 56, 79, .72);
  }

  .top h1 { font-size: 22px; }
  .top p { font-size: 12px; }
  .top-actions { flex-direction: column; align-items: flex-end; gap: 7px; }
  .top-actions button { min-height: 36px; padding: 6px 10px; font-size: 12px; }
  .top .pill { margin: 0; min-height: 32px; padding: 5px 10px; }

  .screen { padding: 15px 13px 20px; }
  .screen-title { margin-bottom: 12px; }
  .screen-title h2 { font-size: 24px; }
  .screen-title p { font-size: 13px; }

  .card,
  .metric-card {
    border-radius: 18px;
    padding: 14px;
  }

  .metric-strip {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 9px;
  }

  .metric-card strong { font-size: 25px; }

  .form-grid,
  .two,
  .three { grid-template-columns: 1fr; }

  button,
  input,
  select,
  textarea,
  .checks label,
  .row { min-height: 46px; }

  .actions { display: grid; grid-template-columns: 1fr; }
  .kv { grid-template-columns: 104px 1fr; font-size: 13px; }
  .checks { grid-template-columns: 1fr; }

  .mobile-bottom-nav {
    position: fixed;
    left: 50%;
    bottom: 10px;
    z-index: 40;
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 5px;
    width: min(calc(100% - 18px), 468px);
    transform: translateX(-50%);
    padding: 8px;
    border: 1px solid rgba(224, 231, 235, .78);
    border-radius: 24px;
    background: rgba(255,255,255,.94);
    box-shadow: 0 24px 55px -30px rgba(19, 56, 79, .75);
    backdrop-filter: blur(14px);
  }

  .mobile-bottom-nav .nav,
  .quick-add-toggle {
    min-height: 52px;
    margin: 0;
    padding: 6px 4px;
    border: 0;
    border-radius: 17px;
    text-align: center;
    color: var(--text-secondary);
    background: transparent;
    font-size: 12px;
  }

  .mobile-bottom-nav .nav.active {
    color: var(--brand-blue);
    background: var(--info-bg);
  }

  .quick-add-toggle {
    position: relative;
    margin-top: -19px;
    min-height: 66px;
    color: #fff;
    background: var(--primary-green);
    box-shadow: 0 16px 24px -16px rgba(47, 158, 118, .9);
  }

  .quick-add-toggle span { display: block; font-size: 25px; line-height: 1; }
  .quick-add-toggle small { display: block; margin-top: 3px; font-weight: 900; }

  .sheet-backdrop:not([hidden]) {
    position: fixed;
    inset: 0;
    z-index: 48;
    display: block;
    background: rgba(13, 34, 49, .32);
  }

  .action-sheet:not([hidden]) {
    position: fixed;
    left: 50%;
    bottom: 86px;
    z-index: 50;
    display: grid;
    gap: 9px;
    width: min(calc(100% - 22px), 460px);
    transform: translateX(-50%);
    padding: 13px;
    border: 1px solid var(--line-2);
    border-radius: 24px;
    background: #fff;
    box-shadow: 0 24px 70px -28px rgba(13, 34, 49, .7);
  }

  .sheet-handle {
    justify-self: center;
    width: 44px;
    height: 5px;
    border-radius: 99px;
    background: #D5E0E6;
  }

  .sheet-title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: var(--brand-blue);
  }

  .sheet-title button { min-height: 36px; padding: 5px 10px; }

  .quick-action {
    display: grid;
    gap: 2px;
    justify-content: flex-start;
    text-align: left;
    border-color: var(--line-2);
    background: var(--cool-gray);
  }

  .quick-action strong { font-weight: 800; }
  .quick-action span { color: var(--text-secondary); font-size: .86rem; font-weight: 400; }
}

/* Master data admin */
.master-note {
  display: flex;
  flex-direction: column;
  gap: 6px;
  border-left: 4px solid var(--brand-green);
}
.master-admin-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}
.admin-card {
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.compact-form {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: #f8fbfc;
}
.compact-form button {
  min-height: 44px;
}
.admin-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.admin-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: #fff;
}
.admin-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}
.admin-actions button {
  min-height: 38px;
  padding: 8px 12px;
}
@media (max-width: 900px) {
  .master-admin-grid,
  .compact-form,
  .admin-row {
    grid-template-columns: 1fr;
  }
  .admin-actions {
    justify-content: flex-start;
  }
}

/* Typography refinement: keep labels and controls clear without making entered data feel heavy. */
.row strong,
.kv strong,
.admin-row strong,
.admin-row .meta,
.card p,
.meta,
.empty-state span,
textarea,
input,
select {
  font-weight: 400;
}

.kv strong {
  color: var(--text-dark);
  line-height: 1.55;
}

.row strong,
.admin-row strong {
  line-height: 1.45;
}

label,
legend,
.card h3,
.screen-title h2,
button,
.status-chip,
.tag {
  font-weight: 700;
}

.primary,
.quick-add-toggle,
.metric-card strong,
.total strong,
.pill,
.section-label,
.brand small,
.brand strong {
  font-weight: 800;
}

/* Friendly form hints for master data labels. */
label small {
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 400;
  line-height: 1.45;
}


.attachment-section {
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.attachment-title {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: flex-start;
  margin-bottom: 10px;
}

.attachment-title h4 {
  margin: 0 0 4px;
  color: var(--text-dark);
}

.attachment-uploader {
  background: #f8fbfc;
  border: 1px solid var(--border-light);
  border-radius: 12px;
  padding: 12px;
  margin-bottom: 10px;
}

.attachment-uploader summary {
  min-height: 44px;
  display: flex;
  align-items: center;
  font-weight: 700;
  color: var(--brand-blue);
  cursor: pointer;
}

.compact-form {
  margin-top: 10px;
}

.attachment-message {
  min-height: 22px;
  margin: 8px 0 12px;
  font-size: .92rem;
  color: var(--text-secondary);
}

.attachment-message[data-kind="success"] { color: var(--safe-green); font-weight: 600; }
.attachment-message[data-kind="error"] { color: #B42318; font-weight: 600; }

.attachment-list {
  display: grid;
  gap: 12px;
}

.attachment-card {
  display: grid;
  grid-template-columns: minmax(92px, 128px) 1fr;
  gap: 14px;
  padding: 12px;
  border: 1px solid var(--border-light);
  border-radius: 14px;
  background: #fff;
}

.attachment-preview {
  display: block;
  overflow: hidden;
  min-height: 92px;
  border-radius: 12px;
  background: var(--cool-gray-bg);
}

.attachment-preview img {
  width: 100%;
  height: 100%;
  min-height: 92px;
  object-fit: cover;
  display: block;
}

.attachment-body {
  min-width: 0;
}

.attachment-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}

.button-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--brand-blue);
  background: #fff;
  text-decoration: none;
  font-weight: 700;
}

@media (max-width: 720px) {
  .attachment-title,
  .attachment-card {
    grid-template-columns: 1fr;
  }
  .attachment-preview,
  .attachment-preview img {
    min-height: 180px;
  }
}


/* Service record creation flow */
.flow-panel {
  margin: 12px 0;
}

.flow-status {
  display: grid;
  gap: 5px;
  padding: 14px;
  border: 1px solid var(--border-light);
  border-radius: 14px;
  background: #fff;
  color: var(--text-secondary);
}

.flow-status strong {
  color: var(--brand-blue);
  font-weight: 800;
}

.flow-status span,
.flow-status small {
  font-weight: 400;
}

.flow-status.info { border-color: #BFDCEC; background: var(--info-bg); }
.flow-status.success { border-color: #B9E4D3; background: #EEF9F4; }
.flow-status.error { border-color: #F2B8B5; background: #FFF4F2; }
.flow-status.error strong { color: #B42318; }
.flow-status.success strong { color: var(--safe-green); }

.section-note {
  display: grid;
  gap: 4px;
  padding: 12px;
  border: 1px solid #BFDCEC;
  border-radius: 12px;
  background: var(--info-bg);
}

.section-note span {
  color: var(--text-secondary);
  font-weight: 400;
}

.create-panel {
  border-left: 4px solid var(--primary-green);
}

.existing-panel {
  border-left: 4px solid var(--brand-blue);
}

.record-groups,
.record-group {
  display: grid;
  gap: 12px;
}

.record-group {
  padding-top: 4px;
}

.record-group + .record-group {
  margin-top: 14px;
  padding-top: 16px;
  border-top: 1px solid var(--border-light);
}

.record-group-title {
  display: grid;
  gap: 3px;
  color: var(--text-secondary);
}

.record-group-title strong {
  color: var(--brand-blue);
  font-weight: 800;
}

.record-group-title span {
  font-weight: 400;
}
