*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #000000);
  --hint: var(--tg-theme-hint-color, #888888);
  --link: var(--tg-theme-link-color, #2481cc);
  --button-bg: var(--tg-theme-button-color, #2481cc);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #f1f1f1);
  --radius: 10px;
  --gap: 12px;
}

html, body {
  height: 100dvh;
  overflow: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.4;
}

/* ── Layout ── */
.view { position: fixed; inset: 0; overflow-y: auto; display: flex; flex-direction: column; padding: 0 0 80px; }
.view[hidden] { display: none !important; }

.header { padding: 16px var(--gap) 8px; font-size: 17px; font-weight: 600; }

/* ── Tab bar ── */
.tab-bar {
  display: flex;
  border-bottom: 1px solid var(--secondary-bg);
  padding: 0 var(--gap);
}
.tab {
  flex: 1;
  padding: 10px 0;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--hint);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: color .15s, border-color .15s;
}
.tab.active {
  color: var(--button-bg);
  border-bottom-color: var(--button-bg);
}

/* ── Panels ── */
.panel { padding: var(--gap); display: flex; flex-direction: column; gap: var(--gap); }
.panel[hidden] { display: none; }

/* ── Form fields ── */
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--hint); }
input, textarea, select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--secondary-bg);
  border-radius: var(--radius);
  background: var(--secondary-bg);
  color: var(--text);
  font-size: 15px;
  outline: none;
  transition: border-color .15s;
}
input:focus, textarea:focus { border-color: var(--button-bg); }
textarea { min-height: 120px; resize: vertical; }

/* ── Client search ── */
.client-search-wrap { position: relative; }
.client-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: var(--bg);
  border: 1px solid var(--secondary-bg);
  border-radius: var(--radius);
  box-shadow: 0 4px 16px rgba(0,0,0,.12);
  z-index: 100;
  max-height: 200px;
  overflow-y: auto;
}
.client-dropdown[hidden] { display: none; }
.dropdown-item {
  padding: 10px 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--secondary-bg);
  font-size: 14px;
}
.dropdown-item:last-child { border-bottom: none; }
.dropdown-item:hover, .dropdown-item:focus { background: var(--secondary-bg); }
.dropdown-item.new-client { color: var(--button-bg); font-weight: 500; }

/* ── New client panel ── */
#new-client-panel {
  background: var(--secondary-bg);
  border-radius: var(--radius);
  padding: var(--gap);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
#new-client-panel[hidden] { display: none; }
.new-client-title { font-size: 13px; font-weight: 600; color: var(--hint); text-transform: uppercase; letter-spacing: .5px; }
.new-client-actions { display: flex; gap: 8px; margin-top: 4px; }

/* ── Selected client display ── */
.selected-client {
  background: var(--secondary-bg);
  border-radius: var(--radius);
  padding: 10px 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
}
.selected-client[hidden] { display: none; }
.selected-client .clear-btn { background: none; border: none; color: var(--hint); cursor: pointer; font-size: 18px; padding: 0 4px; }

/* ── Line items ── */
.line-items { display: flex; flex-direction: column; gap: 8px; }
.line-item {
  display: grid;
  grid-template-columns: 1fr 90px 32px;
  gap: 6px;
  align-items: start;
}
.line-item input { min-width: 0; }
.item-desc-wrap { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.item-desc-wrap select, .item-desc-wrap input { min-width: 0; }
.item-desc-custom[hidden] { display: none; }
.remove-item {
  background: none;
  border: 1px solid var(--secondary-bg);
  border-radius: 6px;
  color: var(--hint);
  font-size: 14px;
  cursor: pointer;
  width: 32px;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-top: 1px;
}
.remove-item:hover { color: #e53935; border-color: #e53935; }

/* ── Buttons ── */
.btn {
  padding: 12px 16px;
  border-radius: var(--radius);
  border: none;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
  transition: opacity .15s;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--button-bg); color: var(--button-text); }
.btn-secondary { background: var(--secondary-bg); color: var(--text); }
.btn-ghost { background: none; border: 1px solid var(--secondary-bg); color: var(--text); }
.btn-sm { padding: 8px 12px; font-size: 13px; }
.btn-link { background: none; border: none; color: var(--button-bg); font-size: 14px; cursor: pointer; padding: 4px 0; }

/* ── Sticky bottom bar ── */
.bottom-bar {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  background: var(--bg);
  border-top: 1px solid var(--secondary-bg);
  padding: 12px var(--gap);
  display: flex;
  gap: 8px;
  z-index: 50;
}
.bottom-bar .btn { flex: 1; }

/* ── Warnings / errors ── */
.warnings {
  background: #fff8e1;
  border: 1px solid #ffe082;
  border-radius: var(--radius);
  padding: 10px 12px;
  font-size: 13px;
  color: #795548;
}
.warnings[hidden] { display: none; }
.field-error { font-size: 12px; color: #e53935; margin-top: 2px; }

/* ── Success view ── */
#view-success {
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 32px var(--gap);
  text-align: center;
}
.success-icon { font-size: 64px; }
.success-title { font-size: 22px; font-weight: 700; }
.success-subtitle { color: var(--hint); font-size: 15px; }

/* ── Spinner ── */
.spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--secondary-bg);
  border-top-color: var(--button-bg);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Confirm view ── */
#view-confirm .section-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--hint);
  text-transform: uppercase;
  letter-spacing: .5px;
  padding: 0 var(--gap);
  margin-top: 8px;
}

/* ── Loading overlay ── */
#view-loading {
  align-items: center;
  justify-content: center;
  gap: 16px;
}
#view-loading p { color: var(--hint); }
