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

:root {
  --color-paper: #dee3e2;
  --color-blush: #fccbcb;
  --color-sky: #78b3d6;
  --color-clay: #d86969;
  --color-forest: #4f7969;
  --color-bg: #dee3e2;
  --color-surface: rgba(255, 255, 255, 0.72);
  --color-surface-strong: #f4f1f1;
  --color-border: #78b3d6;
  --color-text: #315346;
  --color-text-muted: #4f7969;
  --color-primary: #4f7969;
  --color-primary-hover: #3d6154;
  --color-accent: #78b3d6;
  --color-profit: #4f7969;
  --color-loss: #d86969;
  --color-error: #d86969;
  --color-lockout-bg: #fccbcb;
  --color-lockout-border: #d86969;
  --color-focus: #78b3d6;
  --radius: 6px;
  --shadow-sm: 0 12px 30px rgba(79, 121, 105, 0.08);
  --shadow-md: 0 18px 42px rgba(79, 121, 105, 0.14);
  --transition: 0.2s ease;
  --max-width: 1200px;
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--color-text);
  background:
    radial-gradient(circle at top left, rgba(252, 203, 203, 0.18), transparent 34%),
    radial-gradient(circle at top right, rgba(120, 179, 214, 0.12), transparent 30%),
    linear-gradient(180deg, #edf0ef 0%, var(--color-bg) 100%);
  line-height: 1.6;
  min-height: 100vh;
}

[hidden] {
  display: none !important;
}

/* ===== Accessibility ===== */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

*:focus-visible {
  outline: 2px solid var(--color-focus);
  outline-offset: 2px;
}

/* ===== Typography ===== */
h1, h2, h3, h4 {
  font-weight: 600;
  line-height: 1.3;
}

h1 { font-size: 1.75rem; }
h2 { font-size: 1.375rem; }
h3 { font-size: 1.125rem; }
h4 { font-size: 1rem; }

a {
  color: var(--color-primary);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.375rem;
  padding: 0.5rem 1rem;
  font-size: 0.875rem;
  font-weight: 500;
  line-height: 1.5;
  border: 1px solid transparent;
  border-radius: 999px;
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition), color var(--transition), box-shadow var(--transition), transform var(--transition);
  white-space: nowrap;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--color-primary);
  color: #fff;
  border-color: var(--color-primary);
  box-shadow: 0 10px 24px rgba(79, 121, 105, 0.18);
}

.btn-primary:hover:not(:disabled) {
  background: var(--color-primary-hover);
  transform: translateY(-1px);
}

.btn-secondary {
  background: rgba(120, 179, 214, 0.14);
  color: var(--color-text);
  border-color: var(--color-border);
}

.btn-secondary:hover:not(:disabled) {
  background: rgba(120, 179, 214, 0.24);
}

.btn-danger {
  background: var(--color-loss);
  color: #fff;
  border-color: var(--color-loss);
  box-shadow: 0 10px 24px rgba(216, 105, 105, 0.18);
}

.btn-danger:hover:not(:disabled) {
  background: #bf5a5a;
  transform: translateY(-1px);
}

.btn-text {
  background: transparent;
  color: var(--color-text-muted);
  border: none;
  padding: 0.375rem 0.5rem;
}

.btn-text:hover:not(:disabled) {
  color: var(--color-primary);
  background: rgba(120, 179, 214, 0.12);
}

.btn-sm {
  padding: 0.3rem 0.75rem;
  font-size: 0.8125rem;
}

.btn-block {
  display: flex;
  width: 100%;
}

/* ===== Forms ===== */
.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.25rem;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--color-text);
}

.form-group input,
.form-group select,
.form-group textarea {
  display: block;
  width: 100%;
  padding: 0.5rem 0.75rem;
  font-size: 0.9375rem;
  font-family: inherit;
  color: var(--color-text);
  background: rgba(255, 255, 255, 0.84);
  border: none;
  border-radius: var(--radius);
  box-shadow: inset 0 0 0 1px rgba(120, 179, 214, 0.12);
  transition: box-shadow var(--transition), background var(--transition);
}

.form-group input[required],
.form-group select[required],
.form-group textarea[required] {
  box-shadow: inset 0 0 0 1px rgba(216, 105, 105, 0.2);
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  outline: none;
  background: rgba(255, 255, 255, 0.96);
  box-shadow:
    inset 0 0 0 1px rgba(120, 179, 214, 0.28),
    0 0 0 3px rgba(120, 179, 214, 0.16);
}

.form-group textarea {
  resize: vertical;
  min-height: 4rem;
}

.form-group input.invalid,
.form-group select.invalid,
.form-group textarea.invalid {
  box-shadow:
    inset 0 0 0 1px rgba(216, 105, 105, 0.45),
    0 0 0 3px rgba(216, 105, 105, 0.08);
}

.field-error {
  display: block;
  font-size: 0.8125rem;
  color: var(--color-error);
  margin-top: 0.2rem;
  min-height: 1.2em;
}

.form-error {
  color: var(--color-error);
  font-size: 0.875rem;
  margin-bottom: 0.75rem;
  text-align: center;
  white-space: pre-line;
}

.form-row {
  display: grid;
  gap: 1rem;
  grid-template-columns: 1fr;
}

fieldset {
  border: none;
  padding: 0;
  margin: 0;
  min-width: 0;
}

legend {
  padding: 0;
}

.form-actions {
  display: flex;
  gap: 0.75rem;
  margin-top: 1.5rem;
  flex-wrap: wrap;
}

/* ===== Checkbox / Toggle Group ===== */
.checkbox-group {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1rem;
}

.checkbox-group label {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  font-size: 0.875rem;
  font-weight: 400;
  cursor: pointer;
}

.checkbox-group input[type="checkbox"] {
  width: auto;
  margin: 0;
}

/* ===== Login Page ===== */
#page-login {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.login-container {
  max-width: 380px;
  width: min(100%, 380px);
  margin: 0;
  padding: 2.5rem 1.25rem;
  background: #fbfcfb;
  border: 1px solid rgba(120, 179, 214, 0.22);
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(120, 179, 214, 0.08);
}

.login-title {
  font-size: 2.3rem;
  text-align: center;
  margin-bottom: 2rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--color-accent);
}

.lockout-notice {
  background: var(--color-lockout-bg);
  border: 1px solid var(--color-lockout-border);
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
  margin-bottom: 1rem;
  font-size: 0.875rem;
  text-align: center;
}

/* ===== App Shell ===== */
.app-shell {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.app-header {
  background: linear-gradient(135deg, rgba(222, 227, 226, 0.92), rgba(120, 179, 214, 0.18));
  border-bottom: 1px solid rgba(120, 179, 214, 0.28);
  backdrop-filter: blur(10px);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-inner {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 0.75rem 1.25rem;
}

.logo {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--color-accent);
  text-decoration: none;
  flex-shrink: 0;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.logo:hover {
  text-decoration: none;
}

.main-nav {
  flex: 1;
}

.nav-list {
  display: flex;
  list-style: none;
  gap: 0.45rem;
}

.nav-link {
  display: block;
  padding: 0.375rem 0.75rem;
  font-size: 0.875rem;
  color: var(--color-text-muted);
  border-radius: 999px;
  transition: color var(--transition), background var(--transition);
  text-decoration: none;
}

.nav-link:hover,
.nav-link.active {
  color: #fff;
  background: var(--color-accent);
  text-decoration: none;
}

/* ===== App Content ===== */
.app-content {
  flex: 1;
  max-width: var(--max-width);
  width: 100%;
  margin: 0 auto;
  padding: 1.5rem 1.25rem;
}

.view-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
  gap: 0.75rem;
}

.empty-state {
  text-align: center;
  color: var(--color-text-muted);
  padding: 3rem 1rem;
  font-size: 0.9375rem;
}

/* ===== Watch Grid (Dashboard) ===== */
.watch-grid {
  display: grid;
  gap: 0.875rem;
  grid-template-columns: 1fr;
  grid-auto-rows: 8px;
  align-items: start;
}

/* ===== Watch Card ===== */
.watch-card {
  display: block;
  width: auto;
  background: #f7f8f7;
  border: 1px solid rgba(79, 121, 105, 0.08);
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(79, 121, 105, 0.04);
  overflow: hidden;
  transition: box-shadow var(--transition), transform var(--transition);
}

.watch-card:hover {
  box-shadow: 0 6px 18px rgba(120, 179, 214, 0.14);
  transform: translateY(-1px);
}

.watch-card-summary {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1rem;
  cursor: pointer;
  user-select: none;
}

.watch-card-summary:focus-visible {
  outline: 2px solid var(--color-focus);
  outline-offset: -2px;
}

.watch-thumb {
  width: 56px;
  height: 56px;
  border-radius: var(--radius);
  object-fit: cover;
  background: var(--color-bg);
  flex-shrink: 0;
}

.watch-thumb-placeholder {
  width: 56px;
  height: 56px;
  border-radius: var(--radius);
  background: rgba(120, 179, 214, 0.18);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--color-text-muted);
  font-size: 1.25rem;
  flex-shrink: 0;
}

.watch-card-info {
  flex: 1;
  min-width: 0;
}

.watch-card-maker {
  font-size: 0.8125rem;
  color: var(--color-accent);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.watch-card-model {
  font-size: 1rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.watch-card-pnl {
  font-size: 1rem;
  font-weight: 600;
  white-space: nowrap;
  flex-shrink: 0;
}

.watch-card-expand-icon {
  font-size: 0.75rem;
  color: var(--color-text-muted);
  transition: transform var(--transition);
  flex-shrink: 0;
}

.watch-card.expanded .watch-card-expand-icon {
  transform: rotate(180deg);
}

/* ===== Watch Card Detail (Accordion) ===== */
.watch-card-detail {
  display: none;
  border-top: 1px solid var(--color-border);
  padding: 1rem;
}

.watch-card.expanded .watch-card-detail {
  display: block;
}

.detail-section {
  margin-bottom: 1.25rem;
}

.detail-section:last-child {
  margin-bottom: 0;
}

.detail-section-title {
  font-size: 0.8125rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-accent);
  margin-bottom: 0.85rem;
}

.detail-grid {
  display: grid;
  grid-template-columns: 10rem 1fr;
  gap: 0.375rem 1rem;
  font-size: 0.875rem;
}

.detail-label {
  color: var(--color-text-muted);
}

.detail-value {
  font-weight: 500;
}

/* ===== Expense Table ===== */
.expense-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
  margin-top: 0.5rem;
}

.expense-table th,
.expense-table td {
  text-align: left;
  padding: 0.375rem 0.5rem;
  border-bottom: 1px solid var(--color-border);
}

.expense-table th {
  font-weight: 600;
  color: var(--color-accent);
  font-size: 0.8125rem;
}

.expense-table td:last-child,
.expense-table th:last-child {
  text-align: right;
}

/* ===== Image Gallery ===== */
.image-gallery {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.image-gallery img {
  width: 100px;
  height: 100px;
  object-fit: cover;
  border-radius: var(--radius);
  border: 1px solid var(--color-border);
  cursor: pointer;
  transition: opacity var(--transition);
}

.image-gallery img:hover {
  opacity: 0.85;
}

/* ===== Sale Detail ===== */
.sale-detail {
  font-size: 0.875rem;
}

/* ===== Watch Card Actions ===== */
.watch-card-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--color-border);
}

/* ===== P&L Colors ===== */
.pnl-profit {
  color: var(--color-profit);
}

.pnl-loss {
  color: var(--color-loss);
}

.pnl-breakeven {
  color: var(--color-text);
}

/* ===== Portfolio Summary ===== */
.portfolio-stats {
  display: grid;
  gap: 1rem;
  grid-template-columns: 1fr;
  margin-bottom: 2rem;
}

.stat-card {
  background: #fbfcfb;
  border: 1px solid rgba(120, 179, 214, 0.22);
  border-radius: 12px;
  padding: 1.25rem;
  text-align: center;
  box-shadow: 0 2px 8px rgba(120, 179, 214, 0.08);
}

.stat-value {
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 0.25rem;
}

.stat-label {
  font-size: 0.8125rem;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* ===== Filters & Sort Controls ===== */
.controls-bar {
  background: #f7f8f7;
  border: 1px solid rgba(79, 121, 105, 0.08);
  border-radius: 12px;
  padding: 1rem;
  margin-bottom: 1.5rem;
}

.controls-bar summary {
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9375rem;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.controls-bar summary::-webkit-details-marker {
  display: none;
}

.controls-bar summary::before {
  content: "▸";
  transition: transform var(--transition);
}

.controls-bar[open] summary::before {
  transform: rotate(90deg);
}

.controls-grid {
  display: grid;
  gap: 0.75rem;
  grid-template-columns: 1fr;
  margin-top: 1rem;
}

.controls-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

/* ===== Inline Expense List (Form) ===== */
.inline-expense-list {
  margin-top: 0.5rem;
}

.inline-expense-item {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  padding: 0.75rem;
  background: rgba(252, 203, 203, 0.18);
  border: 1px solid rgba(216, 105, 105, 0.22);
  border-radius: var(--radius);
  margin-bottom: 0.5rem;
}

.inline-expense-item .form-group {
  flex: 1;
  margin-bottom: 0;
}

.inline-expense-item .btn {
  margin-top: 1.4rem;
  flex-shrink: 0;
}

.inline-expense-item .exp-remove {
  width: 28px;
  height: 28px;
  min-width: 28px;
  padding: 0;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 1.4rem;
  font-size: 0.8125rem;
  line-height: 1;
}

/* ===== Image Upload ===== */
.image-upload-area {
  border: 2px dashed var(--color-border);
  border-radius: 16px;
  padding: 1.5rem;
  text-align: center;
  cursor: pointer;
  transition: border-color var(--transition), background var(--transition);
  margin-top: 0.5rem;
  background: rgba(120, 179, 214, 0.08);
}

.image-upload-area:hover,
.image-upload-area.dragover {
  border-color: var(--color-accent);
  background: rgba(120, 179, 214, 0.16);
}

.image-upload-area p {
  color: var(--color-text-muted);
  font-size: 0.875rem;
}

.upload-preview-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.75rem;
  margin-bottom: 0.75rem;
}

.upload-preview-item {
  position: relative;
  width: 80px;
  min-height: 80px;
}

.upload-preview-item img {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: var(--radius);
  border: 1px solid var(--color-border);
  display: block;
}

.upload-preview-item .remove-btn {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--color-loss);
  color: #fff;
  border: none;
  font-size: 0.75rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.upload-progress {
  margin-top: 0.5rem;
}

.progress-bar {
  height: 4px;
  background: rgba(120, 179, 214, 0.22);
  border-radius: 2px;
  overflow: hidden;
  margin-top: 0.25rem;
}

.progress-bar-fill {
  height: 100%;
  background: var(--color-accent);
  transition: width 0.3s ease;
  width: 0%;
}

.upload-status {
  font-size: 0.8125rem;
  color: var(--color-text-muted);
}

/* ===== Loading Overlay ===== */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(222, 227, 226, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(4px);
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--color-border);
  border-top-color: var(--color-accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ===== Section Divider ===== */
.section-divider {
  border: none;
  border-top: 1px solid var(--color-border);
  margin: 1.5rem 0;
}

/* ===== Responsive: 768px+ ===== */
@media (min-width: 768px) {
  .watch-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .portfolio-stats {
    grid-template-columns: repeat(4, 1fr);
  }

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

  .controls-grid {
    grid-template-columns: repeat(3, 1fr);
  }

  .detail-grid {
    grid-template-columns: 10rem 1fr;
  }
}

/* ===== Responsive: 1024px+ ===== */
@media (min-width: 1024px) {
  .watch-grid {
    grid-template-columns: repeat(3, 1fr);
  }

  .controls-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

/* ===== Responsive: Small screens ===== */
@media (max-width: 480px) {
  #page-login {
    padding: 1rem;
  }

  .login-container {
    padding: 2rem 1rem;
  }

  .header-inner {
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .nav-list {
    gap: 0;
  }

  .nav-link {
    padding: 0.375rem 0.5rem;
    font-size: 0.8125rem;
  }

  .watch-card-summary {
    padding: 0.75rem;
    gap: 0.75rem;
  }

  .watch-thumb,
  .watch-thumb-placeholder {
    width: 44px;
    height: 44px;
  }

  .view-header {
    flex-direction: column;
    align-items: flex-start;
  }
}

/* ===== Print ===== */
@media print {
  .app-header,
  .loading-overlay,
  .btn,
  .controls-bar {
    display: none;
  }

  .watch-card-detail {
    display: block;
  }
}
