/* === Reset & Base === */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #f5f5f0;
  --surface: #ffffff;
  --border: #d4d4d4;
  --text: #1a1a1a;
  --text-secondary: #555;
  --primary: #2c5f2d;
  --primary-hover: #1e4620;
  --danger: #b91c1c;
  --danger-hover: #991b1b;
  --radius: 6px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

/* === App Layout === */
.app {
  min-height: 100vh;
}

.app-header {
  background: var(--primary);
  color: white;
  padding: 0.75rem 1.5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.app-header h1 {
  font-size: 1.2rem;
  font-weight: 600;
}

.header-actions {
  display: flex;
  gap: 0.5rem;
}

.main-layout {
  display: flex;
  min-height: calc(100vh - 52px);
}

/* === Sidebar === */
.sidebar {
  width: 280px;
  min-width: 280px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 1rem;
  overflow-y: auto;
}

.sidebar-new-btn {
  width: 100%;
  margin-bottom: 1rem;
}

.sidebar h2 {
  font-size: 0.9rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
  margin-bottom: 0.75rem;
}

.invoice-search {
  width: 100%;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.85rem;
  font-family: inherit;
  margin-bottom: 0.5rem;
}

.invoice-sort {
  width: 100%;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.85rem;
  font-family: inherit;
  margin-bottom: 0.5rem;
  background: white;
}

.invoice-sort:focus,
.invoice-search:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(44, 95, 45, 0.15);
}

.invoice-list {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.invoice-list-item {
  padding: 0.6rem 0.75rem;
  border-radius: var(--radius);
  cursor: pointer;
  border: 1px solid transparent;
  transition: background 0.15s;
}

.invoice-list-item:hover {
  background: var(--bg);
}

.invoice-list-item.active {
  background: #e8f0e8;
  border-color: var(--primary);
}

.invoice-list-item .item-name {
  font-weight: 500;
  font-size: 0.9rem;
}

.invoice-list-item .item-meta {
  font-size: 0.78rem;
  color: var(--text-secondary);
}

/* === Content === */
.content {
  flex: 1;
  padding: 1.5rem 2rem;
  max-width: 900px;
}

.panel h2 {
  font-size: 1.3rem;
  margin-bottom: 1rem;
}

.panel h3 {
  font-size: 1rem;
  margin-bottom: 0.5rem;
  color: var(--text-secondary);
}

.form-section {
  margin-bottom: 1.5rem;
  padding: 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 0.75rem;
  margin-bottom: 0.5rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.form-group.full-width {
  grid-column: 1 / -1;
}

.form-group label {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-secondary);
}

.form-group input,
.form-group select,
.form-group textarea {
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-family: inherit;
  background: white;
}

.form-group input:disabled,
.form-group select:disabled,
.form-group textarea:disabled,
.items-table td input:disabled {
  background: #f0f0f0;
  color: var(--text-secondary);
  border-color: #e0e0e0;
  cursor: not-allowed;
}

.form-group input:focus,
.form-group select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(44, 95, 45, 0.15);
}

/* === Buttons === */
.btn {
  padding: 0.45rem 1rem;
  border: none;
  border-radius: var(--radius);
  font-size: 0.85rem;
  font-family: inherit;
  cursor: pointer;
  font-weight: 500;
  transition: background 0.15s, opacity 0.15s;
}

.btn-primary {
  background: var(--primary);
  color: white;
}

.btn-primary:hover {
  background: var(--primary-hover);
}

.btn-secondary {
  background: #e8e8e8;
  color: var(--text);
}

.btn-secondary:hover {
  background: #d8d8d8;
}

.btn-ghost {
  background: transparent;
  color: white;
  border: 1px solid rgba(255,255,255,0.4);
}

.btn-ghost:hover {
  background: rgba(255,255,255,0.1);
}

.btn-danger {
  background: var(--danger);
  color: white;
}

.btn-danger:hover {
  background: var(--danger-hover);
}

.btn-icon {
  background: none;
  border: none;
  cursor: pointer;
  padding: 0.25rem;
  font-size: 1.1rem;
  color: var(--text-secondary);
  border-radius: 4px;
}

.btn-icon:hover {
  background: #f0f0f0;
  color: var(--danger);
}

/* === Items Table === */
.items-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 0.75rem;
}

.items-table th {
  text-align: left;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 0.4rem 0.5rem;
  border-bottom: 2px solid var(--border);
}

.items-table td {
  padding: 0.3rem 0.25rem;
  vertical-align: top;
}

.items-table .col-date { width: 130px; }
.items-table .col-code { width: 100px; }
.items-table .col-amount { width: 110px; }
.items-table .col-actions { width: 40px; }

.items-table td input,
.items-table td select {
  width: 100%;
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.85rem;
  font-family: inherit;
}

.items-table td input:focus,
.items-table td select:focus {
  outline: none;
  border-color: var(--primary);
}

.items-table td input.amount-input {
  text-align: right;
}

.items-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.total-display {
  font-size: 1.1rem;
  padding-right: 3rem;
}

.form-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
}

/* === Code Dropdown === */
.code-cell {
  position: relative;
}

.code-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 100;
  background: white;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  max-height: 220px;
  overflow-y: auto;
  width: 360px;
}

.code-dropdown-item {
  padding: 0.4rem 0.6rem;
  cursor: pointer;
  font-size: 0.83rem;
  display: flex;
  gap: 0.5rem;
}

.code-dropdown-item:hover,
.code-dropdown-item.highlighted {
  background: #e8f0e8;
}

.code-dropdown-item .code-num {
  font-weight: 600;
  min-width: 40px;
}

.code-dropdown-item .code-desc {
  color: var(--text-secondary);
}

.code-dropdown-item .code-price {
  margin-left: auto;
  font-weight: 500;
}

/* === Date Picker === */
.date-cell {
  position: relative;
}

.datepicker {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 200;
  background: white;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  padding: 0.5rem;
  width: 240px;
  user-select: none;
}

.datepicker-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.4rem;
}

.datepicker-header span {
  font-size: 0.85rem;
  font-weight: 600;
}

.datepicker-header button {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1rem;
  padding: 0.15rem 0.4rem;
  border-radius: 4px;
  color: var(--text);
}

.datepicker-header button:hover {
  background: var(--bg);
}

.datepicker-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 1px;
  text-align: center;
}

.datepicker-grid .dp-weekday {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-secondary);
  padding: 0.2rem 0;
}

.datepicker-grid .dp-day {
  font-size: 0.8rem;
  padding: 0.3rem 0;
  border-radius: 4px;
  cursor: pointer;
}

.datepicker-grid .dp-day:hover {
  background: #e8f0e8;
}

.datepicker-grid .dp-day.today {
  font-weight: 700;
  color: var(--primary);
}

.datepicker-grid .dp-day.selected {
  background: var(--primary);
  color: white;
}

.datepicker-grid .dp-day.other-month {
  color: #ccc;
}

/* === Patients Table === */
.patients-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 1rem;
}

.patients-table th {
  text-align: left;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 0.5rem 0.5rem;
  border-bottom: 2px solid var(--border);
}

.patients-table td {
  padding: 0.45rem 0.5rem;
  font-size: 0.88rem;
  border-bottom: 1px solid var(--border);
}

.patients-table tr:hover {
  background: var(--bg);
}

.patients-table .patient-actions {
  white-space: nowrap;
  text-align: right;
}

.patients-table .patient-actions button {
  margin-left: 0.25rem;
}

.patients-empty {
  color: var(--text-secondary);
  font-size: 0.85rem;
  padding: 1rem 0;
}

/* === Settings Panel === */
#settings-panel .form-grid {
  margin-bottom: 1rem;
}

#settings-panel h3 {
  margin-top: 1rem;
}

/* === Login Page === */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
  width: 100%;
  max-width: 360px;
}

.login-card h1 {
  font-size: 1.2rem;
  margin-bottom: 1.5rem;
  color: var(--primary);
}

.login-btn {
  width: 100%;
  margin-top: 1rem;
}

.login-error {
  color: var(--danger);
  font-size: 0.85rem;
  margin-top: 0.5rem;
}

/* === Validation === */
.field-error {
  border-color: var(--danger) !important;
  box-shadow: 0 0 0 2px rgba(185, 28, 28, 0.15) !important;
}

.field-error-msg {
  font-size: 0.75rem;
  color: var(--danger);
  margin-top: 0.15rem;
}

.items-table-error {
  border: 1px solid var(--danger);
  border-radius: var(--radius);
}

/* === Responsive === */
@media (max-width: 768px) {
  .main-layout {
    flex-direction: column;
  }

  .sidebar {
    width: 100%;
    min-width: 0;
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: 200px;
  }

  .content {
    padding: 1rem;
  }

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