/* =================================================================
   TEMA Y VARIABLES GENERALES (Inspirado en macOS)
   ================================================================= */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

:root {
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  
  /* Tema Oscuro (Dark Mode) */
  --bg-dark: #1e1e1e;
  --bg-glass-dark: rgba(38, 38, 38, 0.75);
  --bg-component-dark: rgba(56, 56, 56, 0.5);
  --border-dark: rgba(80, 80, 80, 0.7);
  --text-primary-dark: #f5f5f7;
  --text-secondary-dark: #b7bdc9;
  --blue-vivid: #00a9ff;
  --green-vivid: #00d67f;
  --red-vivid: #ff4d5f;
  --orange-vivid: #ffad0a;
  --accent-gradient: linear-gradient(135deg, #009dff, #23e1ff);
  --radius: 12px;
  --transition: all 0.2s ease-in-out;
  --scrollbar-track: rgba(56, 56, 56, 0.45);
  --scrollbar-thumb: rgba(183, 189, 201, 0.4);
  --scrollbar-thumb-hover: rgba(183, 189, 201, 0.62);
}

html[data-theme='light'] {
  --bg: #f5f5f7;
  --bg-glass: rgba(242, 242, 247, 0.8);
  --bg-component: #ffffff;
  --border: #d1d1d6;
  --text-primary: #1d1d1f;
  --text-secondary: #6e6e73;
  --scrollbar-track: rgba(209, 209, 214, 0.45);
  --scrollbar-thumb: rgba(110, 110, 115, 0.4);
  --scrollbar-thumb-hover: rgba(110, 110, 115, 0.62);
}

html[data-theme='dark'] {
  --bg: var(--bg-dark);
  --bg-glass: var(--bg-glass-dark);
  --bg-component: var(--bg-component-dark);
  --border: var(--border-dark);
  --text-primary: var(--text-primary-dark);
  --text-secondary: var(--text-secondary-dark);
}

* { box-sizing: border-box; }

* {
  scrollbar-width: thin;
  scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);
}

*::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

*::-webkit-scrollbar-track {
  background: var(--scrollbar-track);
}

*::-webkit-scrollbar-thumb {
  background: var(--scrollbar-thumb);
  border-radius: 999px;
  border: 2px solid var(--scrollbar-track);
}

*::-webkit-scrollbar-thumb:hover {
  background: var(--scrollbar-thumb-hover);
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: var(--font-sans);
  background-color: var(--bg);
  background-image: linear-gradient(180deg, var(--bg) 0%, #1c1c1c 100%);
  color: var(--text-primary);
  min-height: 100%;
  margin: 0;
  overflow-x: hidden;
  line-height: 1.4;
}

html[data-theme='light'] body {
  background-image: none;
}

/* Ocultar etiquetas visualmente pero mantenerlas accesibles */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

/* =================================================================
   CONTENEDOR PRINCIPAL Y EFECTO "GLASS"
   ================================================================= */
.dashboard {
  width: 100%;
  background: var(--bg);
  padding: 30px 40px;
  min-height: 100dvh;
  overflow: visible;
  display: flex;
  flex-direction: column;
}

h1 {
  text-align: left;
  font-size: 1.8rem;
  font-weight: 700;
  margin: 0 0 20px 0;
  color: var(--text-primary);
}

h3 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-primary);
  margin: 12px 0 8px 0;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border);
}

/* =================================================================
   COMPONENTES INTERNOS (Summary, Form, History)
   ================================================================= */
.summary {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
  margin-bottom: 20px;
  background-color: var(--bg-component);
  border-radius: var(--radius);
  padding: 16px 24px;
  border: 1px solid var(--border);
  width: 100%;
}
.summary div { 
  text-align: center; 
  font-size: 1rem;
  color: var(--text-secondary);
}
.summary div strong {
  display: block;
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text-primary);
  margin-top: 5px;
}

.main-layout {
  display: flex;
  gap: 30px;
  flex: 1;
  min-height: 0;
  width: 100%;
}

.main-layout__left {
  flex: 1.2;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.entry-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  background-color: var(--bg-component);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}

.main-layout__right {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 15px;
  background-color: var(--bg-component);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
}

.main-layout__right h3 {
  width: 100%;
}

.main-layout__left > h3 {
  border-bottom: none;
  padding-bottom: 0;
}

.chart-block {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.chart-block + .chart-block {
  margin-top: 6px;
  padding-top: 12px;
}

.form {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 0;
  align-items: center;
}

#concept {
  grid-column: span 6;
}

#amount,
#type,
#category {
  grid-column: span 2;
}

.date-field {
  position: relative;
  grid-column: span 2;
  height: 42px;
  min-height: 42px;
  overflow: hidden;
}

#date {
  display: block;
  width: 100%;
  height: 100%;
  min-height: 100%;
  padding-right: 40px;
}

.date-field__hint {
  position: absolute;
  left: 12px;
  right: 42px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-secondary);
  font-size: 0.9rem;
  pointer-events: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  opacity: 1;
  transition: opacity 0.15s ease-in-out;
  line-height: 1;
}

#date[data-empty='true']::-webkit-datetime-edit {
  color: transparent;
}

#date[data-empty='false'] + .date-field__hint,
#date:focus + .date-field__hint {
  opacity: 0;
}

#addBtn {
  grid-column: span 2;
}

#resetBtn {
  grid-column: span 2;
}

input, select {
  padding: 11px 12px;
  background: var(--bg-component);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-primary);
  font-size: 0.9rem;
  transition: var(--transition);
  width: 100%;
  min-height: 42px;
}
input::placeholder { color: var(--text-secondary); }
input:focus, select:focus {
  outline: none;
  border-color: #007aff;
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.3);
}

select {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23999' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 32px;
}

select option {
  background-color: var(--bg-component);
  color: var(--text-primary);
}

html[data-theme='dark'] select option {
  background-color: #2a2a2a;
  color: #f5f5f7;
}

html[data-theme='light'] select option {
  background-color: #ffffff;
  color: #1d1d1f;
}
input[aria-invalid='true'],
select[aria-invalid='true'] {
  border-color: #ff453a;
  box-shadow: 0 0 0 3px rgba(255, 69, 58, 0.2);
}

.form-error {
  margin: 0;
  min-height: 16px;
  color: #ff6b5f;
  font-size: 0.85rem;
}

button {
  padding: 11px 16px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 600;
  transition: var(--transition);
  min-height: 42px;
}

#addBtn {
  background-color: var(--bg-component);
  color: var(--blue-vivid);
  border: 1px solid var(--border);
}
#addBtn:hover {
  background: var(--accent-gradient);
  color: white;
  border-color: transparent;
  box-shadow: 0 8px 20px rgba(0, 169, 255, 0.35);
}

.reset-btn {
  background-color: var(--bg-component);
  color: var(--red-vivid);
  border: 1px solid var(--border);
  grid-column: span 2;
}
.reset-btn:hover {
  background-color: var(--red-vivid);
  color: white;
  border-color: var(--red-vivid);
  box-shadow: 0 8px 20px rgba(255, 77, 95, 0.33);
}

/* Estilos para los nuevos botones de control */
.controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 0;
}
.controls button {
  grid-column: span 1;
  padding: 10px 15px;
  font-size: 0.9rem;
}

.backup-btn {
  background-color: var(--bg-component);
  color: var(--green-vivid);
  border: 1px solid var(--border);
}
.backup-btn:hover {
  background-color: var(--green-vivid);
  color: white;
  border-color: var(--green-vivid);
  box-shadow: 0 8px 20px rgba(0, 214, 127, 0.3);
}
.restore-btn {
  background-color: var(--bg-component);
  color: var(--orange-vivid);
  border: 1px solid var(--border);
}
.restore-btn:hover {
  background-color: var(--orange-vivid);
  color: white;
  border-color: var(--orange-vivid);
  box-shadow: 0 8px 20px rgba(255, 173, 10, 0.3);
}


/* =================================================================
   HISTORIAL Y GRÁFICO
   ================================================================= */
ul {
  list-style: none;
  padding: 0;
  margin: 0;
  background-color: var(--bg-component);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  overflow-y: auto;
  flex: 1;
  min-height: 60px;
}
li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 0.88rem;
  color: var(--text-secondary);
}
li:last-child {
  border-bottom: none;
}

/* NUEVOS ESTILOS PARA LA LISTA */
li > div:first-child {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
li strong {
  color: var(--text-primary);
  font-weight: 500;
  font-size: 0.9rem;
  overflow-wrap: anywhere;
}
li .actions {
  display: flex;
  gap: 5px;
}
li .action-btn {
  background: transparent;
  color: var(--text-secondary);
  font-size: 0.85rem;
  padding: 4px;
  margin: 0;
  grid-column: auto;
  border-radius: 50%;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
}
li .action-btn:focus-visible {
  outline: 2px solid #007aff;
  outline-offset: 2px;
}
li .edit-btn:hover {
  background-color: rgba(0, 122, 255, 0.15);
  color: #007aff;
}
li .delete-btn:hover {
  background-color: rgba(255, 69, 58, 0.15);
  color: #ff453a;
}
.empty-state {
  text-align: center;
  padding: 24px 16px;
  color: var(--text-secondary);
  justify-content: center;
  font-size: 0.9rem;
}


#chart {
  display: block;
  width: 100% !important;
  height: auto !important;
  max-height: 250px !important;
  margin: 5px auto 15px;
  background: transparent;
  border: none;
}

#categoryChart {
  display: block;
  width: 100% !important;
  height: auto !important;
  max-height: 250px !important;
  margin: 5px auto;
  background: transparent;
  border: none;
}

@media (min-width: 901px) {
  html,
  body {
    height: 100dvh;
    overflow: hidden;
  }

  .dashboard {
    height: 100dvh;
    min-height: 100dvh;
    padding: 20px 32px 18px;
    overflow: hidden;
  }

  h1 {
    margin-bottom: 14px;
  }

  .summary {
    margin-bottom: 14px;
    padding: 12px 18px;
  }

  .main-layout {
    gap: 24px;
    min-height: 0;
  }

  .entry-panel {
    padding: 12px;
    gap: 8px;
  }

  .main-layout__right {
    padding: 16px 18px;
    gap: 10px;
    overflow: hidden;
  }

  .main-layout__right .chart-block {
    flex: 1;
    min-height: 0;
  }

  .main-layout__right .chart-block + .chart-block {
    margin-top: 0;
    padding-top: 10px;
  }

  #chart,
  #categoryChart {
    max-height: clamp(200px, 27vh, 290px) !important;
  }
}

.chart-status {
  margin: 0;
  min-height: 0;
  color: var(--orange-vivid);
  font-size: 0.9rem;
  font-weight: 600;
  text-align: center;
  letter-spacing: 0.2px;
}

.chart-status:empty {
  display: none;
}

.chart-status:not(:empty) {
  display: block;
  margin: 10px 2px 2px;
  min-height: 20px;
}

html[data-theme='light'] #chart {
  background: transparent;
  box-shadow: none;
}

/* Responsive */
@media (max-width: 1024px) {
  .dashboard {
    padding: 24px;
    min-height: 100dvh;
  }
  .summary {
    gap: 12px;
    padding: 14px 16px;
  }
  .main-layout {
    gap: 20px;
  }
  .main-layout__right {
    padding: 18px;
  }
}

@media (max-width: 900px) {
  .main-layout {
    flex-direction: column;
    min-height: auto;
  }
  .main-layout__left,
  .main-layout__right {
    width: 100%;
  }
  ul {
    max-height: none;
    overflow: visible;
    flex: initial;
  }
  #chart,
  #categoryChart {
    max-width: 320px;
  }
}

@media (max-width: 768px) {
  .dashboard {
    padding: 18px 16px 16px;
    min-height: auto;
  }
  .main-layout {
    gap: 24px;
  }
  .main-layout__right {
    padding: 20px 16px;
    gap: 20px;
  }
  .chart-block {
    gap: 6px;
  }
  .main-layout__right h3 {
    margin: 0;
    padding-bottom: 8px;
  }
  .summary {
    grid-template-columns: 1fr;
  }
  .form {
    grid-template-columns: 1fr 1fr;
  }
  #concept {
    grid-column: span 2;
  }
  #amount,
  #type,
  #category,
  .date-field,
  #addBtn,
  #resetBtn {
    grid-column: span 1;
  }
  .controls {
    grid-template-columns: 1fr;
  }
  .controls button {
    grid-column: span 1;
  }
  h1 {
    font-size: 1.55rem;
    margin-bottom: 16px;
  }
  li {
    padding: 11px 10px;
  }
  #chart,
  #categoryChart {
    max-width: 290px;
  }
  #chart {
    margin: 8px auto 14px;
  }
  #categoryChart {
    margin: 8px auto 2px;
  }
}

@media (max-width: 480px) {
  .dashboard {
    padding:
      calc(14px + env(safe-area-inset-top))
      calc(12px + env(safe-area-inset-right))
      calc(14px + env(safe-area-inset-bottom))
      calc(12px + env(safe-area-inset-left));
  }
  .main-layout {
    gap: 18px;
  }
  .main-layout__right {
    background: transparent;
    border: none;
    padding: 0;
    gap: 12px;
  }
  .chart-block {
    background-color: var(--bg-component);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px 12px;
    gap: 8px;
  }
  .chart-block + .chart-block {
    margin-top: 0;
    padding-top: 14px;
  }
  .main-layout__right h3 {
    margin: 2px 0 0;
  }
  .form {
    grid-template-columns: 1fr;
    gap: 10px;
  }
  #concept,
  #amount,
  #type,
  #category,
  .date-field,
  #addBtn,
  .reset-btn {
    grid-column: span 1;
  }
  input,
  select,
  button {
    font-size: 16px;
  }
  .form input,
  .form select,
  .form button {
    height: 48px;
    min-height: 48px;
  }
  .date-field__hint {
    font-size: 16px;
  }
  .date-field {
    height: 48px;
    min-height: 48px;
  }
  #date {
    height: 100%;
    min-height: 100%;
    line-height: normal;
    -webkit-appearance: auto;
    appearance: auto;
  }
  #date::-webkit-date-and-time-value {
    text-align: left;
  }
  #date:invalid::-webkit-datetime-edit {
    color: var(--text-secondary);
  }
  #date::-webkit-calendar-picker-indicator {
    margin: 0;
    opacity: 0.9;
  }
  .summary div {
    font-size: 1rem;
  }
  h1 {
    font-size: 1.35rem;
    margin-bottom: 12px;
  }
  ul {
    max-height: none;
    overflow: visible;
    flex: initial;
  }
  #chart,
  #categoryChart {
    max-width: min(100%, 320px);
    max-height: 320px !important;
  }
  #chart {
    margin: 8px auto 12px;
  }
  #categoryChart {
    margin: 8px auto 0;
  }
}
