.spreadsheet-list {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.spreadsheet-card {
  display: flex;
  align-items: center;
  gap: 1rem;
  background: var(--color-surface);
  border: 1px solid var(--color-border-light);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  transition: border-color 0.15s;

  &:hover {
    border-color: var(--color-primary);
  }
}

.spreadsheet-card-link {
  flex: 1;
  text-decoration: none;
  color: inherit;

  & h2 {
    font-size: 1rem;
    font-weight: 600;
  }

  & p {
    font-size: 0.875rem;
    color: var(--color-text-muted);
  }

  & time {
    font-size: 0.75rem;
    color: var(--color-text-muted);
  }
}

.spreadsheet-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 0.75rem;
}

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

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

.formula-bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  margin-bottom: 0.5rem;
}

.cell-reference {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  font-weight: 600;
  min-width: 3rem;
  text-align: center;
  padding: 0.25rem 0.5rem;
  background: var(--color-header);
  border-radius: 4px;
}

.formula-input {
  flex: 1;
  border: none;
  outline: none;
  font-family: var(--font-mono);
  font-size: 0.85rem;
  padding: 0.25rem;
}

.spreadsheet-body {
  flex: 1;
  display: flex;
  gap: 0;
  overflow: hidden;
}

.grid-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.grid-wrapper {
  flex: 1;
  overflow: auto;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-surface);
}

/* --- Custom Functions Panel --- */

.functions-panel {
  width: 300px;
  border: 1px solid var(--color-border);
  border-left: none;
  border-radius: 0 var(--radius) var(--radius) 0;
  background: var(--color-surface);
  display: flex;
  flex-direction: column;
  overflow: hidden;

  &.hidden {
    display: none;
  }
}

.functions-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--color-border-light);
  background: var(--color-header);

  & h3 {
    font-size: 0.85rem;
    font-weight: 600;
    margin: 0;
  }
}

.functions-panel-body {
  flex: 1;
  display: flex;
  overflow: hidden;
}

.functions-editor {
  width: 100%;
  flex: 1;
  resize: none;
  border: none;
  outline: none;
  padding: 0.5rem 0.75rem;
  font-family: var(--font-mono);
  font-size: 0.75rem;
  line-height: 1.6;
  tab-size: 2;
  background: var(--color-surface);
  color: var(--color-text);

  &::placeholder {
    color: var(--color-text-muted);
    opacity: 0.5;
  }
}

.functions-panel-footer {
  padding: 0.4rem 0.75rem;
  border-top: 1px solid var(--color-border-light);
  background: var(--color-header);
}

.functions-panel-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.25rem;
}

.functions-status {
  font-size: 0.7rem;
  font-family: var(--font-mono);
}

.functions-status-success {
  color: #2e7d32;
}

.functions-status-error {
  color: #c62828;
}

/* --- Style Rules Panel --- */

.style-panel {
  width: 300px;
  border: 1px solid var(--color-border);
  border-left: none;
  border-radius: 0 var(--radius) var(--radius) 0;
  background: var(--color-surface);
  display: flex;
  flex-direction: column;
  overflow: hidden;

  &.hidden {
    display: none;
  }
}

.style-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--color-border-light);
  background: var(--color-header);

  & h3 {
    font-size: 0.85rem;
    font-weight: 600;
    margin: 0;
  }
}

.style-panel-close {
  background: none;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  color: var(--color-text-muted);
  padding: 0 0.25rem;
  line-height: 1;

  &:hover {
    color: var(--color-text);
  }
}

.style-panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
}

.style-rules-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.style-rule {
  border: 1px solid var(--color-border-light);
  border-radius: 4px;
  padding: 0.4rem 0.5rem;
  font-size: 0.75rem;
  background: var(--color-header);

  &:hover {
    border-color: var(--color-border);
  }
}

.style-rule-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.2rem;
}

.style-rule-range {
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: 0.75rem;
  color: var(--color-primary);
}

.style-rule-delete {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  color: var(--color-text-muted);
  padding: 0 0.2rem;

  &:hover {
    color: #c62828;
  }
}

.style-rule-expression,
.style-rule-format {
  cursor: pointer;

  & code {
    font-family: var(--font-mono);
    font-size: 0.7rem;
    color: var(--color-text-muted);
    word-break: break-all;
  }

  &:hover code {
    color: var(--color-text);
  }
}

.style-rule-format code {
  color: var(--color-primary);
}

.style-rule-format::before {
  content: "fmt ";
  font-size: 0.6rem;
  font-weight: 600;
  color: var(--color-primary);
  opacity: 0.6;
}

.style-rule-form {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--color-border-light);
}

.style-rule-form-row {
  display: flex;
  gap: 0.25rem;
}

.style-rule-range-input,
.style-rule-expression-input {
  width: 100%;
  padding: 0.3rem 0.5rem;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 0.75rem;
  outline: none;

  &:focus {
    border-color: var(--color-primary);
  }
}

.style-rule-range-input {
  flex: 1;
}

.btn-fill-range {
  padding: 0.3rem 0.5rem;
  font-size: 0.8rem;
  line-height: 1;
}

.style-rule-form-actions {
  display: flex;
  gap: 0.35rem;
}

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

  &:hover {
    opacity: 0.9;
  }
}

.btn-sm {
  padding: 0.25rem 0.6rem;
  font-size: 0.75rem;
}

.style-panel-footer {
  padding: 0.4rem 0.75rem;
  border-top: 1px solid var(--color-border-light);
  background: var(--color-header);
}

.style-hint {
  font-size: 0.65rem;
  color: var(--color-text-muted);
  margin: 0;
  line-height: 1.5;

  & code {
    font-family: var(--font-mono);
    font-size: 0.6rem;
    background: var(--color-surface);
    padding: 0.1rem 0.25rem;
    border-radius: 2px;
  }
}

.spreadsheet-grid {
  border-collapse: collapse;
  table-layout: fixed;
  width: max-content;
  min-width: 100%;
}

.spreadsheet-grid th,
.spreadsheet-grid td {
  border: 1px solid var(--color-border-light);
  padding: 0;
  font-size: 0.8rem;
}

.col-header,
.row-header {
  background: var(--color-header);
  color: var(--color-text-muted);
  font-weight: 500;
  font-size: 0.75rem;
  text-align: center;
  user-select: none;
  position: sticky;
}

.col-header {
  top: 0;
  z-index: 2;
  padding: 0.35rem 0.5rem;
  min-width: 100px;
  cursor: pointer;

  &:hover { background: var(--color-border-light); }
  &.header-selected {
    background: var(--color-primary);
    color: white;
  }
}

.row-header {
  left: 0;
  z-index: 1;
  padding: 0.25rem 0.5rem;
  width: 50px;
  min-width: 50px;
  cursor: pointer;

  &:hover { background: var(--color-border-light); }
  &.header-selected {
    background: var(--color-primary);
    color: white;
  }
}

.corner-header {
  z-index: 3;
  cursor: pointer;

  &:hover { background: var(--color-border-light); }
}

.cell {
  height: 28px;
  min-width: 100px;
  cursor: cell;
  position: relative;

  &.selected {
    outline: 2px solid var(--color-primary);
    outline-offset: -1px;
    background: var(--color-selected);
    z-index: 1;
  }

  &.in-range {
    background: rgba(26, 115, 232, 0.08);
    border-color: rgba(26, 115, 232, 0.25);
  }

  &.editing {
    outline: 2px solid #1a73e8;
    outline-offset: -1px;
    background: var(--color-surface);
    box-shadow: inset 0 0 0 1px #1a73e8;
    z-index: 2;
    cursor: text;
  }

  &.copy-source {
    outline: 2px dashed var(--color-primary);
    outline-offset: -1px;
    z-index: 1;
  }

  &.cut-source {
    outline: 2px dashed #e67c00;
    outline-offset: -1px;
    opacity: 0.5;
    z-index: 1;
  }
}

/* Conditional formatting styles */
.cell-style-danger   { background-color: #fce4ec; color: #c62828; }
.cell-style-warning  { background-color: #fff8e1; color: #f57f17; }
.cell-style-success  { background-color: #e8f5e9; color: #2e7d32; }
.cell-style-info     { background-color: #e3f2fd; color: #1565c0; }
.cell-style-highlight { background-color: #fff9c4; }
.cell-style-muted    { color: #9e9e9e; }
.cell-style-bold     { font-weight: 700; }
.cell-style-italic   { font-style: italic; }

.status-bar {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 1rem;
  padding: 0.35rem 0.75rem;
  background: var(--color-header);
  border: 1px solid var(--color-border);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  font-size: 0.75rem;
  font-family: var(--font-mono);
  color: var(--color-text-muted);
  min-height: 28px;

  &.hidden {
    display: none;
  }
}

.status-item {
  white-space: nowrap;

  & strong {
    color: var(--color-text);
    font-weight: 600;
  }
}

.cell-display {
  display: block;
  padding: 0.2rem 0.4rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: var(--font-mono);
  font-size: 0.8rem;
  line-height: 1.4;
}

.cell-inline-editor {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: none;
  outline: none;
  background: var(--color-surface);
  font-family: var(--font-mono);
  font-size: 0.8rem;
  padding: 0.2rem 0.4rem;
  box-sizing: border-box;
  z-index: 3;
}

/* Help button */
.btn-help {
  font-weight: 700;
  font-size: 0.9rem;
  min-width: 32px;
  justify-content: center;
}

/* Help dialog */
.help-dialog {
  border: none;
  border-radius: var(--radius);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.2);
  padding: 0;
  max-width: 680px;
  width: 90vw;
  max-height: 80vh;

  &[open] {
    display: flex;
    flex-direction: column;
  }

  &::backdrop {
    background: rgba(0, 0, 0, 0.4);
  }
}

.help-dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--color-border-light);
  background: var(--color-header);
  border-radius: var(--radius) var(--radius) 0 0;

  & h2 {
    font-size: 1rem;
    font-weight: 600;
    margin: 0;
  }
}

.help-dialog-body {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 1.25rem;
}

/* Markdown rendered content */
.markdown-body {
  font-size: 0.85rem;
  line-height: 1.6;

  & h1 { display: none; }

  & h2 {
    font-size: 1rem;
    font-weight: 700;
    margin: 1.5rem 0 0.5rem;
    padding-bottom: 0.3rem;
    border-bottom: 1px solid var(--color-border-light);

    &:first-of-type { margin-top: 0; }
  }

  & h3 {
    font-size: 0.9rem;
    font-weight: 600;
    margin: 1rem 0 0.4rem;
  }

  & p { margin: 0.4rem 0; }

  & table {
    width: 100%;
    border-collapse: collapse;
    margin: 0.5rem 0;
    font-size: 0.8rem;
  }

  & th, & td {
    border: 1px solid var(--color-border-light);
    padding: 0.35rem 0.6rem;
    text-align: left;
  }

  & th {
    background: var(--color-header);
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
    color: var(--color-text-muted);
  }

  & code {
    font-family: var(--font-mono);
    font-size: 0.78rem;
    background: var(--color-header);
    padding: 0.1rem 0.35rem;
    border-radius: 3px;
  }

  & pre {
    background: var(--color-header);
    border: 1px solid var(--color-border-light);
    border-radius: 4px;
    padding: 0.6rem 0.8rem;
    overflow-x: auto;
    margin: 0.5rem 0;

    & code {
      background: none;
      padding: 0;
      font-size: 0.78rem;
    }
  }

  & ul, & ol {
    padding-left: 1.5rem;
    margin: 0.4rem 0;
  }

  & li { margin: 0.2rem 0; }
}

/* Image cells */
.cell-image {
  height: auto;
  min-height: 28px;

  & .cell-display {
    padding: 2px;
    display: flex;
    align-items: center;
    justify-content: center;

    & img {
      max-width: 100%;
      max-height: 120px;
      object-fit: contain;
      border-radius: 2px;
    }
  }
}
